开发者社区> 问答> 正文

硒不刷新詹金斯页面

我有一个测试用例,它像这样:

打开主页 如果没有内容 刷新页面 继续其他步骤... 这是代码的相关部分:

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++;
        }
    }
}

展开
收起
祖安文状元 2020-01-08 15:22:35 357 0
1 条回答
写回答
取消 提交回答
  • 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秒后找不到该方法,

    2020-01-08 15:22:46
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
3D动画的菜谱式灯光与云渲染 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载