如何运行跨浏览器测试-在BrowserStack上进行硒测试

时间:2020-06-12 09:40:14

标签: selenium selenium-webdriver 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;
    }

2 个答案:

答案 0 :(得分:2)

要在BrowserStack上运行测试,只需更改中心URL,如果您能够在本地进行测试,那么您的测试也将在BrowserStack上运行。

要遵循的步骤-

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

2)如链接中所述,通过测试脚本中的所需功能:https://www.browserstack.com/automate/capabilities

您可以在此处参考文档:https://www.browserstack.com/docs?product=automate

此外,他们的GitHub存储库应为您提供帮助:https://github.com/browserstack

答案 1 :(得分:2)

答案

对此链接的引用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);

}