我是硒测试的新手。我想在多个浏览器上运行selenium test cases
,反对internet explorer, Firefox, opera和chrome。我必须遵循什么方法。你们能告诉我最好的流程是什么吗?
selenium web driver是否支持多个浏览器??
我们写了登录脚本。它在火狐、chrome和ie浏览器上都能成功运行。但我想运行它为那些多个浏览器顺序。
web driver支持多浏览器当然也支持移动端
ChromeDriver
IEDiver
FirefoxDriver
OperaDriver
AndroidDriver
下面是一个在多个浏览器中运行相同测试的示例。
package ma.glasnost.test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
.........
DesiredCapabilities[] browserList = {DesiredCapabilities.chrome(),DesiredCapabilities.firefox(),DesiredCapabilities.internetExplorer(), DesiredCapabilities.opera()};
for (DesiredCapabilities browser : browserList)
{
try {
System.out.println("Testing in Browser: "+browser.getBrowserName());
driver = new RemoteWebDriver(new URL("http://127.0.0.1:8080/..."), browser);
希望对你有帮助。
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Sample {
private WebDriver _driver;
@Test
public void IEconfiguration() throws Exception {
System.setProperty("webdriver.ie.driver",
"D:/Softwares/Selenium softwares/drivers/IEDriverServer.exe");
_driver = new InternetExplorerDriver();
login();
}
@Test
public void FFconfiguration() throws Exception {
_driver = new FirefoxDriver();
login();
}
@Test
public void CRconfiguration() throws Exception {
System.setProperty("webdriver.chrome.driver",
"D:/Softwares/Selenium softwares/drivers/chromedriver.exe");
_driver = new ChromeDriver();
//_driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
login();
}
public void login() throws Exception {
_driver.get("http://www.google.com");
}
}
在此之前,我们必须安装chrome和internet explorer驱动程序。exe文件并运行它们。
你可以使用WebDriver扩展框架的JUnitRunner
下面是一个测试谷歌搜索"Hello World"的例子
@RunWith(WebDriverRunner.class)
@Firefox
@Chrome
@InternetExplorer
public class WebDriverExtensionsExampleTest {
// Model
@FindBy(name = "q")
WebElement queryInput;
@FindBy(name = "btnG")
WebElement searchButton;
@FindBy(id = "search")
WebElement searchResult;
@Test
public void searchGoogleForHelloWorldTest() {
open("http://www.google.com");
assertCurrentUrlContains("google");
type("Hello World", queryInput);
click(searchButton);
waitFor(3, SECONDS);
assertTextContains("Hello World", searchResult);
}
}
确保在maven pom.xml依赖项中添加WebDriver Extensions框架
<dependency>
<groupId>com.github.webdriverextensions</groupId>
<artifactId>webdriverextensions</artifactId>
<version>1.2.1</version>
</dependency>
驱动程序可以使用提供的maven插件下载。只需添加
<plugin>
<groupId>com.github.webdriverextensions</groupId>
<artifactId>webdriverextensions-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<goals>
<goal>install-drivers</goal>
</goals>
</execution>
</executions>
<configuration>
<drivers>
<driver>
<name>internetexplorerdriver</name>
<version>2.44</version>
</driver>
<driver>
<name>chromedriver</name>
<version>2.12</version>
</driver>
</drivers>
</configuration>
</plugin>
到您的pom.xml。或者,如果您喜欢手动下载它们,只需用
注释测试类@DriverPaths(chrome="path/to/chromedriver", internetExplorer ="path/to/internetexplorerdriver")
注释指向驱动程序。
注意,上面的例子使用了WebDriver Extensions Bot类中的静态方法,以使测试更具可读性。但是,您并不一定要使用它们。在纯Selenium WebDriver中重写的上述测试将如下所示
@Test
public void searchGoogleForHelloWorldTest() throws InterruptedException {
WebDriver driver = WebDriverExtensionsContext.getDriver();
driver.get("http://www.google.com");
assert driver.getCurrentUrl().contains("google");
queryInput.sendKeys("Hello World");
searchButton.click();
SECONDS.sleep(3);
assert searchResult.getText().contains("Hello World");
}