github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/webdriver/product_test.go (about)

     1  // +build large
     2  
     3  package webdriver
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  	"testing"
     9  
    10  	mapset "github.com/deckarep/golang-set"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/tebeka/selenium"
    13  	"github.com/web-platform-tests/wpt.fyi/shared"
    14  )
    15  
    16  func TestProductParam_Order(t *testing.T) {
    17  	runWebdriverTest(t, func(t *testing.T, app AppServer, wd selenium.WebDriver) {
    18  		t.Run("Order", func(t *testing.T) {
    19  			testProductParamSets(
    20  				t, wd, app,
    21  				[]string{"chrome", "firefox"},
    22  				[]string{"firefox", "chrome"})
    23  		})
    24  
    25  		t.Run("Labels", func(t *testing.T) {
    26  			testProductParamSets(
    27  				t, wd, app,
    28  				[]string{"chrome[stable]"},
    29  				[]string{"firefox[experimental]", "chrome"})
    30  		})
    31  
    32  		t.Run("SHA", func(t *testing.T) {
    33  			t.Run("Latest", func(t *testing.T) {
    34  				testProductParamSets(t, wd, app, []string{"chrome@latest"})
    35  			})
    36  		})
    37  
    38  		t.Run("Specific", func(t *testing.T) {
    39  			testProductParamSets(t, wd, app,
    40  				[]string{fmt.Sprintf("chrome@%s", StaticTestDataRevision[:7])},
    41  				[]string{fmt.Sprintf("firefox@%s", StaticTestDataRevision)},
    42  			)
    43  		})
    44  	})
    45  }
    46  
    47  func testProductParamSets(t *testing.T, wd selenium.WebDriver, app AppServer, productSpecs ...[]string) {
    48  	for _, specs := range productSpecs {
    49  		t.Run(strings.Join(specs, ","), func(t *testing.T) {
    50  			testProducts(t, wd, app, specs...)
    51  		})
    52  	}
    53  }
    54  
    55  func testProducts(
    56  	t *testing.T,
    57  	wd selenium.WebDriver,
    58  	app AppServer,
    59  	productSpecs ...string) {
    60  	// Navigate to the wpt.fyi homepage.
    61  	products, _ := shared.ParseProductSpecs(productSpecs...)
    62  	filters := shared.TestRunFilter{
    63  		Labels:   mapset.NewSetWith(shared.MasterLabel),
    64  		Products: products,
    65  	}
    66  	path := fmt.Sprintf("/results?%s", filters.ToQuery().Encode())
    67  	if err := wd.Get(app.GetWebappURL(path)); err != nil {
    68  		assert.FailNow(t, fmt.Sprintf("Failed to load %s: %s", path, err.Error()))
    69  	}
    70  
    71  	// Wait for the results view to load.
    72  	runsLoadedCondition := func(wd selenium.WebDriver) (bool, error) {
    73  		testRuns, err := getTestRunElements(wd, "wpt-results")
    74  		if err != nil {
    75  			return false, err
    76  		}
    77  		return len(testRuns) > 0, nil
    78  	}
    79  	if err := wd.WaitWithTimeout(runsLoadedCondition, LongTimeout); err != nil {
    80  		assert.FailNow(t, fmt.Sprintf("Error waiting for test runs: %s", err.Error()))
    81  	}
    82  
    83  	// Check loaded test runs
    84  	testRuns, err := getTestRunElements(wd, "wpt-results")
    85  	if err != nil {
    86  		assert.FailNow(t, fmt.Sprintf("Failed to get test runs: %s", err.Error()))
    87  	}
    88  
    89  	assertProducts(t, wd, testRuns, products...)
    90  
    91  	// Wait for the actual results to load.
    92  	resultsLoadedCondition := func(wd selenium.WebDriver) (bool, error) {
    93  		pathParts, err := getPathPartElements(wd, "wpt-results")
    94  		if err != nil {
    95  			return false, err
    96  		}
    97  		return len(pathParts) > 0, nil
    98  	}
    99  	err = wd.WaitWithTimeout(resultsLoadedCondition, LongTimeout)
   100  	assert.Nil(t, err)
   101  }
   102  
   103  func assertProducts(t *testing.T, wd selenium.WebDriver, testRuns []selenium.WebElement, products ...shared.ProductSpec) {
   104  	if len(testRuns) != len(products) {
   105  		assert.Failf(t, "Incorrect number of runs", "Expected %v TestRun(s).", len(products))
   106  		return
   107  	}
   108  	for i, product := range products {
   109  		args := []interface{}{testRuns[i]}
   110  		browserNameBytes, _ := wd.ExecuteScriptRaw("return arguments[0].testRun.browser_name", args)
   111  		browserName, _ := ExtractScriptRawValue(browserNameBytes, "value")
   112  		assert.Equal(t, product.BrowserName, browserName.(string))
   113  		if product.Labels != nil {
   114  			labelBytes, _ := wd.ExecuteScriptRaw("return arguments[0].testRun.labels", args)
   115  			labels, _ := ExtractScriptRawValue(labelBytes, "value")
   116  			for label := range product.Labels.Iter() {
   117  				assert.Contains(t, labels, label)
   118  			}
   119  		}
   120  	}
   121  }