tha*_*guy 6 java testng selenium cross-browser
所以目前我正在做这样的事情来进行跨浏览器测试:
@DataProvider(name="foo")
public Object[][] getDrivers() {
DesiredCapabilities firefoxCapabs = DesiredCapabilities.firefox();
capabillities.setCapability("version", "26");
capabillities.setCapability("platform", Platform.WINDOWS);
DesiredCapabilities chromeCapabs = ....
....
DesiredCapabilities ieCapabs = ...
....
return new Object[][]{
{new RemoteWebDriver(url, firefoxCapabs)},
{new RemoteWebDriver(url, chromeCapabs)},
......
};
}
@Test(dataProvider="foo")
public void testSomething(WebDriver driver) {
//some test
}
Run Code Online (Sandbox Code Playgroud)
这似乎非常低效,因为我每次运行测试时基本上都是在创建和销毁这些WebDriver对象.至少在TestSuite级别没有办法做这样的事情,这样我就不会为每次测试生成和销毁这些对象.我想要像下面这样的东西.我知道你不能拥有@BeforeSuite方法的DataProvider!
public class TestSuite{
public static WebDriver driver;
@BeforeSuite(dataProvider="foo")
public void setDriver(WebDriver driver) {
this.driver = driver;
}
}
public class TestClass {
private WebDriver driver;
@BeforeTest
public void getDriver() {
this.driver = TestSuite.driver;
}
@Test
public void myTest() {
//use this.driver to do testing stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我有没有看到做这样的事情的选择?
Sauce Labs On Demand 有一个很棒的 Jenkins 插件 ( https://saucelabs.com/jenkins/5 )。他们的方法非常简单:您选中/取消选中要测试的操作系统和浏览器,然后 Jenkins 设置环境变量供您的测试选择。下面是使用 Spring 的 @Configuration 的完整示例:
package com.acme.test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
@Configuration
public class SauceLabsWebDriverConfiguration {
@Autowired private Environment environment;
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public WebDriver webDriver() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("version", environment.getProperty("SELENIUM_VERSION", "17.0.1"));
capabilities.setCapability("platform", environment.getProperty("SELENIUM_PLATFORM", "XP"));
capabilities.setCapability("browserName", environment.getProperty("SELENIUM_BROWSER", "firefox"));
String username = environment.getProperty("SAUCE_USER_NAME", "enter_your_username_here");
String accessKey = environment.getProperty("SAUCE_API_KEY", "enter_your_api_here");
return new RemoteWebDriver(new URL("http://" + username + ":" + accessKey + "@ondemand.saucelabs.com:80/wd/hub"), capabilities);
}
}
Run Code Online (Sandbox Code Playgroud)
Sauce Labs 有一些免费计划,但如果您不想使用它们,您应该能够切换掉构造 URL 的最后部分(“http://”+ 用户名 +“:”+ accessKey +“@ ondemand.saucelabs.com:80/wd/hub") 您想要指向的实际服务器 URL (" http://mydomain.com ")。
技巧基本上是用环境提供的名称替换硬编码的浏览器/功能名称,然后让您的构建运行程序(ant/maven/等)为您想要测试和“循环”的每个操作系统/浏览器组合设置环境变量那些以某种方式。SauceLabs 插件让循环变得简单。如果您想运行简单的本地测试,您仍然可以提供默认后备值。
// Before
DesiredCapabilities firefoxCapabs = DesiredCapabilities.firefox();
capabillities.setCapability("version", "26");
capabillities.setCapability("platform", Platform.WINDOWS);
// After
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("version", environment.getProperty("SELENIUM_VERSION", "17.0.1"));
capabilities.setCapability("platform", environment.getProperty("SELENIUM_PLATFORM", "XP"));
capabilities.setCapability("browserName", environment.getProperty("SELENIUM_BROWSER", "firefox"));
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你。
归档时间: |
|
查看次数: |
6402 次 |
最近记录: |