TestNG、Cucumber 和 Selenium 中的多个浏览器并行测试时每个线程如何获取浏览器名称
How to get broweser name when each thread in parallel test execution with multiple browsers in TestNG, Cucumber and Selenide
有没有办法使用 Selenide 运行 获取每个线程的当前浏览器名称?
我将 TestNG Cucumber 与多个浏览器集成。
所以我需要知道在@After hook,哪个浏览器是运行测试
并基于浏览器做一些事情。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="8" parallel="tests" >
<test name="ChromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.site.runner.TestRunner">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="com.site.runner.TestRunner">
</class>
</classes>
</test>
<test name="SafariTest">
<parameter name="browser" value="safari" />
<classes>
<class name="com.site.runner.TestRunner">
</class>
</classes>
</test>
</suite>
Selenide set driver
public static synchronized void setDriver(String browser) {
setBrowser(browser);
}
public String setBrowser(String browser){
switch(browser){
case "chrome":
return setCapabilities(browser);
case "firefox":
return Configuration.browser = "firefox";
case "edge":
return Configuration.browser = "edge";
default:
return "No Browser found";
}
public static void directToBrowserUrl(String browserUrl) {
open(browserUrl);
}
获取@After
方法中driver
的capabilities
和current thread
代码:
Capabilities capabilities = ((RemoteWebDriver) driver).getCapabilities();
String browserName = capabilities.getBrowserName();
String threadName = Thread.currentThread().getName();
long threadId = Thread.currentThread().getId();
System.out.println("Thread name & browser name: "+threadName +" --> "+browserName);
System.out.println("Thread id & browser name: "+threadId +" --> "+browserName);
if(browserName.equalsIngnoreCase("Chrome"){
//do your stuff
}
输出:
Thread name & browser name: com.CallerScript.Caller.main() --> chrome
Thread id & browser name: 16 --> chrome
有没有办法使用 Selenide 运行 获取每个线程的当前浏览器名称?
我将 TestNG Cucumber 与多个浏览器集成。
所以我需要知道在@After hook,哪个浏览器是运行测试
并基于浏览器做一些事情。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="8" parallel="tests" >
<test name="ChromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.site.runner.TestRunner">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="com.site.runner.TestRunner">
</class>
</classes>
</test>
<test name="SafariTest">
<parameter name="browser" value="safari" />
<classes>
<class name="com.site.runner.TestRunner">
</class>
</classes>
</test>
</suite>
Selenide set driver
public static synchronized void setDriver(String browser) {
setBrowser(browser);
}
public String setBrowser(String browser){
switch(browser){
case "chrome":
return setCapabilities(browser);
case "firefox":
return Configuration.browser = "firefox";
case "edge":
return Configuration.browser = "edge";
default:
return "No Browser found";
}
public static void directToBrowserUrl(String browserUrl) {
open(browserUrl);
}
获取@After
方法中driver
的capabilities
和current thread
代码:
Capabilities capabilities = ((RemoteWebDriver) driver).getCapabilities();
String browserName = capabilities.getBrowserName();
String threadName = Thread.currentThread().getName();
long threadId = Thread.currentThread().getId();
System.out.println("Thread name & browser name: "+threadName +" --> "+browserName);
System.out.println("Thread id & browser name: "+threadId +" --> "+browserName);
if(browserName.equalsIngnoreCase("Chrome"){
//do your stuff
}
输出:
Thread name & browser name: com.CallerScript.Caller.main() --> chrome
Thread id & browser name: 16 --> chrome