仅在无头浏览器模式下,Selenium网格测试用例失败
普通浏览器模式 [测试成功]
无头浏览器模式 [测试失败]
我在下面运行无头浏览器
xvfb-run java -Dwebdriver.firefox.marionette =“/ tmp / geckodriver”-Dwebdriver.firefox.bin =“/ tmp / firefox / firefox”-jar selenium-server-standalone-3.0.1.jar -role webdriver -hub http://192.168.1.106:4444/grid/register -port 5566 -host 172.18.0.60
测试用例代码:
package example;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.net.MalformedURLException;
public class NewTest
{
public WebDriver driver;
public String URL, Node;
private JavascriptExecutor jse;
private String baseUrl;
protected ThreadLocal<RemoteWebDriver> threadDriver = null;
@Parameters({"browser","url"})
@BeforeTest
public void launchapp(String browser,String url) throws MalformedURLException
{
baseUrl = "http://myhost";
if (browser.equalsIgnoreCase("firefox"))
{
System.out.println(" Executing on FireFox");
String Node = url;
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");
driver = new RemoteWebDriver(new URL(Node), cap);
// Puts an Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
// Launch website
driver.navigate().to(baseUrl);
driver.manage().window().maximize();
}
else
{
throw new IllegalArgumentException("The Browser Type is Undefined");
}
jse = (JavascriptExecutor)driver;
}
@Test
public void functionalityTest() throws InterruptedException
{
driver.findElement(By.linkText("Sign In")).click();
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("xxxxx@xxx.xxx");
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys("xxxxxx");
driver.findElement(By.id("send2")).click();
jse.executeScript("scroll(0, 250);");
driver.findElement(By.cssSelector(".customerlink a")).click();
Thread.sleep(1000);
driver.findElement(By.linkText("My Account")).click();
Thread.sleep(2000);
jse.executeScript("scroll(0, 250);");
Thread.sleep(1000);
jse.executeScript("scroll(0, -250);");
Thread.sleep(2000);
driver.findElement(By.cssSelector(".customerlink a")).click();
Thread.sleep(1000);
driver.findElement(By.linkText("Sign Out")).click();
Thread.sleep(2000);
}
@AfterTest
public void closeBrowser()
{
driver.quit();
}
}
我在无头brwoser模式的第一行中出现异常
07:16:53.571 INFO - Executing: [new session: Capabilities [{marionette=true, browserName=firefox, version=, platform=ANY}]])
07:16:53.580 INFO - Creating a new session for Capabilities [{marionette=true, browserName=firefox, version=, platform=ANY}]
07:17:00.268 INFO - Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
07:17:02.648 INFO - Detected dialect: OSS
07:17:02.665 INFO - Done: [new session: Capabilities [{marionette=true, firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@3e74110c, browserName=firefox, moz:firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@3e74110c, version=, platform=ANY}]]
07:17:02.701 INFO - Executing: [implicit wait: 50000])
07:17:02.717 INFO - Done: [implicit wait: 50000]
07:17:02.724 INFO - Executing: [get: http://myhost])
07:18:03.564 INFO - Done: [get: http://myhost]
07:18:03.570 INFO - Executing: [maximise window])
07:18:03.580 INFO - Done: [maximise window]
07:18:03.593 INFO - Executing: [find element: By.linkText: Sign In])
07:18:53.917 WARN - Exception thrown
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Sign In"}
喜欢
错误状态显然无法定位元素,但它在正常模式下如何工作?
使用无头浏览器模式的代码是否有任何限制?
答案 0 :(得分:0)
终于发现,Headless浏览器没有考虑driver.manage().window().maximize();
所以我通过xvfb-run命令设置屏幕大小
-s“-screen 0,1920x1080x24”
示例:
xvfb-run -s“-screen 0,1920x1080x24”java -Dwebdriver.firefox.marionette =“/ tmp / geckodriver”-Dwebdriver.firefox.bin =“/ tmp / firefox / firefox”-jar selenium-server- standalone-3.0.1.jar -role webdriver -hub http://192.168.1.106:4444/grid/register -port 5566 -host 172.18.0.60
信息:我正在使用bootstrap,因此可以根据屏幕大小更改元素的可见性。所以它会抛出元素未找到的异常