我正在使用Selenium Webdiver在不同的浏览器上运行我的Selenium测试,但是它们都需要交互模式来运行测试,我们可以在没有交互模式的情况下运行这些测试吗?
答案 0 :(得分:0)
有几种解决方案:
1。 您可以使用无头测试方法,尝试使用HtmlUnitDriver,PhantomJSDriver,
下面是无头测试的代码示例(尽管在Java中,我看到您正在使用c#进行搜索),但这可能会有所帮助
HTMLdriver
@Test
public void runHtmlHeadless() {
String URl = "https://www.google.com";
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get(URl);
System.err.println("TITLE: " + driver.getTitle());
}
Chrome
@Test
public void createChromeDriverHeadless() throws IOException
{
System.setProperty("webdriver.chrome.driver", "/Users/user/Downloads/chromedriver");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("//Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.navigate().to("https://the-internet.herokuapp.com/login");
WebDriverWait waitForUsername = new WebDriverWait(driver, 5000);
waitForUsername.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
driver.findElement(By.id("username")).sendKeys("tomsmith");
driver.findElement(By.cssSelector("button.radius")).click();
WebDriverWait waitForError = new WebDriverWait(driver, 5000);
waitForError.until(ExpectedConditions.visibilityOfElementLocated(By.id("flash")));
Assert.assertTrue(driver.findElement(By.id("flash")).getText().contains("Your password is invalid!"));
driver.quit();
}
希望这会有所帮助,