我有一个类似这样的测试用例:
这是代码的相关部分:
public JpoPO() {
driver.get(Settings.JPO_TEST_URL);
PageFactory.initElements(driver, this);
System.out.println("[INFO] Homepage initialized.");
zatvoriModal();
refreshIfNeeded();
zatvoriModal();
(new WebDriverWait(driver, 30)).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("#loading")));
System.out.println("[DEBUG] broj .ng-scope elemenata: " +driver.findElements(By.cssSelector(".ng-scope")).size());
System.out.println("[OK] JpoPO() initialized.");
}
这是refreshIfNeeded()
部分:
public void refreshIfNeeded() {
if(System.getProperty("os.name").equals("Linux")){
System.out.println("### A"+now());
int broj = driver.findElements(By.cssSelector(".ng-scope")).size();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("location.reload()");
//driver.findElement(By.cssSelector("header")).sendKeys(Keys.F5);
driver.get(driver.getCurrentUrl());
waitForNoSpinner();
System.out.println("[DEBUG] location reloaded, .ng-scope elements: "+broj);
System.out.println("### B"+now());
}else{
System.out.println("[] Starting refreshIfNeeded()");
Date ts1 = new Date();
int count = 0;
while (driver.findElements(By.cssSelector(".ng-scope")).size()==0 && count < 10){
driver.navigate().refresh();
zatvoriModal();
try {
(new WebDriverWait(driver, 10)).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector(".ng-scope"), 0));
Date ts2 = new Date();
long trajanje = ts2.getTime() - ts1.getTime();
System.out.println(String.format("[INFO] Učitavanje sadržaja: %s ms.", trajanje));
} catch (Exception e){
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.out.println("[] Count: "+count);
count++;
}
}
}
在我的 Windows 机器上本地运行 时,该功能正常运行,但是在 Jenkins远程Linux 机器上运行时,刷新功能却没有似乎没有用。
这是Jenkins控制台的输出:
16:18:48 [INFO] Homepage initialized.
16:18:48 ### A2019-10-30T16:18:48.904
16:18:49 [INFO] Spinner (ili uvjet čekanja) je trajao: 117 ms. (waitForNoSpinner-try)
16:18:49 [INFO] Spinner (ili uvjet čekanja) je trajao: 89 ms. (waitForNoSpinner-finally)
16:18:49 [DEBUG] location reloaded, .ng-scope elements: 0
16:18:49 ### B2019-10-30T16:18:49.417
16:18:49 [DEBUG] broj .ng-scope elemenata: 0
16:18:49 [OK] JpoPO() initialized.
在尝试查找除非刷新页面之前不存在的元素时,测试在以下步骤中失败。 使用无法共享的屏幕快照也可以确认这一点。
有多种使用Selenium here刷新页面的方法,但是它们都不起作用。
似乎js.executeScript("location.reload()")
和driver.navigate().refresh()
都不起作用。
我正在使用以下Chromedriver选项:
if(System.getProperty("os.name").equals("Linux")){
options.addArguments("--headless");
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
options.addArguments("--window-size=1200,800");
WebDriverManager.chromedriver().version("77.0.3865.40").setup();
}
编辑:
当我尝试使用Robot类进行刷新时,会得到一个java.awt.AWTException: headless environment
,所以我在代码中添加了System.setProperty("java.awt.headless", "false");
。
现在我得到
java.awt.AWTError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
我几乎不了解Linux,这可能是原因吗?
答案 0 :(得分:0)
您在刷新之前存储.ng-scope
元素的大小,此后不获取大小。这就是您在日志中获得0的原因。您应该在waitForNoSpinner
之后搜索元素以获取更新大小值:
if(System.getProperty("os.name").equals("Linux")){
System.out.println("### A"+now());
int broj = driver.findElements(By.cssSelector(".ng-scope")).size();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("location.reload()");
//driver.findElement(By.cssSelector("header")).sendKeys(Keys.F5);
driver.get(driver.getCurrentUrl());
waitForNoSpinner();
broj = wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector(".ng-scope"), 0)).size();
System.out.println("[DEBUG] location reloaded, .ng-scope elements: "+ broj);
System.out.println("### B"+now());
}
这是另一种refreshIfNeeded
方法,其中waitForNoSpinner
和zatvoriModal
基于操作系统。如果waitForNoSpinner
和zatvoriModal
之间没有区别,请用其中之一替换if
。如果20秒后找不到TimeoutException
,则方法将抛出.ng-scope
:
public void refreshIfNeeded() {
WebDriverWait wait = new WebDriverWait(driver, 20, 1000);
System.out.println("[DEBUG] check .ng-scope elements and refresh if not located");
wait.until(d -> {
if (d.findElements(By.cssSelector(".ng-scope")).isEmpty()) {
System.out.println("[DEBUG] no .ng-scope elements, refresh..");
driver.navigate().refresh();
waitForNoSpinner();
/*if (System.getProperty("os.name").equals("Linux")) {
waitForNoSpinner();
} else {
zatvoriModal();
}*/
}
return !d.findElements(By.cssSelector(".ng-scope")).isEmpty();
});
System.out.println("[DEBUG] .ng-scope elements: " + driver.findElements(By.cssSelector(".ng-scope")).size());
}
有循环:
public void refreshIfNeeded() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 20, 1000);
System.out.println("[DEBUG] check .ng-scope elements and refresh if not located");
for (int i=0; i < 10; i++) {
try {
wait.until(d -> {
if (d.findElements(By.cssSelector(".ng-scope")).isEmpty()) {
System.out.println("[DEBUG] no .ng-scope elements, refresh..");
driver.navigate().refresh();
if (System.getProperty("os.name").equals("Linux")) {
waitForNoSpinner();
} else {
zatvoriModal();
}
}
return !d.findElements(By.cssSelector(".ng-scope")).isEmpty();
});
break;
} catch (Exception e) {
Thread.sleep(3000);
}
}
}