我有一个测试用例,它像这样:
打开主页 如果没有内容 刷新页面 继续其他步骤... 这是代码的相关部分:
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++;
}
}
}
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基于OS。如果两者之间没有区别waitForNoSpinner,请zatvoriModal用if其中之一替换。TimeoutException如果.ng-scope20秒后找不到该方法,
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。