如何 运行 跨浏览器测试 - BrowserStack 上的 Selenium 测试

How To Run Cross Browser Testing - Selenium Tests On BrowserStack

我正在使用页面对象模型,我想 运行 在浏览器堆栈上进行跨浏览器测试。我卡在 BeforeTest 方法中,dataProvider 的 bcoz 不与 BeforeTest 一起使用。

    public static void setup() throws MalformedURLException {
        browserStack(); 
    }


    @Test (dataProvider = "browserStackData")
    public static void browserStack(Platform platform,String browserName,String browserVersion) throws MalformedURLException {

        DesiredCapabilities caps = new DesiredCapabilities();

        caps.setPlatform(platform);
        caps.setBrowserName(browserName);
        caps.setBrowserName(browserVersion);
        caps.setCapability("project", "WebAPP");
        caps.setCapability("build", "1.0");
        caps.setCapability("name", "Login");
        caps.setCapability("browserstack.local", "false");
        caps.setCapability("browserstack.networkLogs", "true");

        driver = new RemoteWebDriver(new URL(URL), caps);
    }

    @DataProvider(name = "browserStackData" , parallel = true)
    public Object[][]getData() {
        Object[][] testData = new Object[][]{
                {Platform.MAC, "chrome", "84"},
                {Platform.WIN10, "firefox", "78"},
                {Platform.MAC, "safari", "13.1"}
        };
        return testData;
    }

为了 运行 在 BrowserStack 上进行测试,您只需要更改集线器 URL,如果您能够在本地进行测试,那么您的测试也会在 BrowserStack 上 运行。

要遵循的步骤-

1) 将 BrowserStack Hub URL 指定为: “https://” + 用户名 + “:” + AUTOMATE_KEY + “@hub-cloud.browserstack.com/wd/hub”;

2) 如 link 中所述,在测试脚本中传递 Desired Capabilities:https://www.browserstack.com/automate/capabilities

您可以参考这里的文档:https://www.browserstack.com/docs?product=automate

此外,他们的 GitHub 存储库应该可以帮助您:https://github.com/browserstack

回答

参考这个 link : https://www.browserstack.com/guide/how-to-setup-browserstack-automate

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="2" name="Suite" verbose="2" parallel="tests">

    <test name="Test on Chrome">
        <parameter name="browser" value="Chrome"/>
        <parameter name="browserVersion" value="83.0"/>
        <parameter name="os" value="Windows"/>
        <parameter name="osVersion" value="10"/>
        <classes>
            <class name="com.TC01Login"/>
            <class name="com.TC02Dashboard"/>
        </classes>
    </test>

    <test name="Test on Firefox">
        <parameter name="browser" value="Chrome"/>
        <parameter name="browserVersion" value="83.0"/>
        <parameter name="os" value="Windows"/>
        <parameter name="osVersion" value="10"/>
        <classes>
            <class name="com.TC01Login"/>
            <class name="com.TC02Dashboard"/>
        </classes>
    </test>

    <test name="Test on Safari">
        <parameter name="browser" value="Safari"/>
        <parameter name="browserVersion" value="13.0"/>
        <parameter name="os" value="OS X"/>
        <parameter name="osVersion" value="Catalina"/>
        <classes>
            <class name="com.TC01Login"/>
            <class name="com.TC02Dashboard"/>
        </classes>
    </test>

</suite>




@Parameters({"browser","browserVersion","os","osVersion"})
@BeforeTest
public static void browserStack(String browser, String browserVersion, String os,String osVersion) throws MalformedURLException {

    DesiredCapabilities caps = new DesiredCapabilities();

    caps.setCapability("browser",browser);
    caps.setCapability("browserVersion",browserVersion);
    caps.setCapability("os",os);
    caps.setCapability("osVersion",osVersion);

    caps.setCapability("project", "xyz");
    caps.setCapability("build", "1.0");
    caps.setCapability("name", "Login");
    caps.setCapability("browserstack.local", "false");
    caps.setCapability("browserstack.networkLogs", "true");
    caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

    driver = new RemoteWebDriver(new URL(URL), caps);

}