硒網格測試情況下,無頭的瀏覽器模式下模擬瀏覽器測試案例失敗 - selemium電網
正常的瀏覽器模式 [試車成功]
- 硒獨立服務器失敗:3.0.1
- 火狐:38.0.1
- IP:192.168.1.40
無頭瀏覽器模式 [測試失敗]
- 硒獨立服務器:3.0.1
- 火狐:38.0
- IP:172.18.0.60
我運行模擬瀏覽器像下面
xvfb的-運行java -Dwebdriver.firefox.marionette = 「/ TMP/geckodriver」 -Dwebdriver.firefo x.bin = 「/ tmp目錄/火狐/火狐」 的罐子硒的服務器獨立-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("[email protected]");
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, [email protected]0c, browserName=firefox, moz:[email protected]0c, 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"}
像
錯誤狀態顯然無法定位元素,但它是如何在正常模式下工作的?
使用無頭瀏覽器模式的代碼有沒有限制?
我可以知道在上面的哪裏,你正在使用「無頭瀏覽器」的概念?據我所知,無頭瀏覽器是在「HtmlUnitDriver」中實現的,可能是NodeJSDriver(這個我還沒試過)。我在這裏錯過了任何概念嗎? – Uday
哥們,我太新了!我相信無頭模式是在沒有瀏覽器UI的情況下運行硒測試。我的瀏覽器運行在xvfb而不是UI模式,所以我相信它是無頭模式。爲您的簡單參考http://agiletesting.blogspot.in/2016/01/running-selenium-webdriver-tests-using.html –
看起來像這樣xvfb在Linux/Ubuntu環境中工作,而不是在Windows上。發現xnest也適用於Linux/Ubuntu env。發現這個鏈接'http://stackoverflow.com/questions/944086/is-there-anything-like-xvfb-or-xnest-for-windows'的Windows版本。 – Uday