Unit testing Selenium—等待事件

Unit testing Selenium—等待事件,unit-testing,testing,selenium,Unit Testing,Testing,Selenium,我需要创建一个测试用例,它将有一个facebook登录。 那么,我如何才能强制测试用户登录facebook以继续测试 以下是我想要的程序: 用户点击一个链接 它重定向到facebook登录 检查页面的内容 使用以下方法。 1) 找到要单击的链接的css选择器 String cssSelector = ..blablalba..; //现在你可以点击你找到的链接了 driver.findElement(By.cssSelector(cssSelector)).click(); //或者直接使用

我需要创建一个测试用例,它将有一个facebook登录。 那么,我如何才能强制测试用户登录facebook以继续测试

以下是我想要的程序:

  • 用户点击一个链接

  • 它重定向到facebook登录

  • 检查页面的内容


  • 使用以下方法。 1) 找到要单击的链接的css选择器

    String cssSelector = ..blablalba..;
    
    //现在你可以点击你找到的链接了

    driver.findElement(By.cssSelector(cssSelector)).click();
    
    //或者直接使用js:

    JavascriptExecutor js = (JavascriptExecutor) driver;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("var x = $(\'"+cssSelector+"\');");
            stringBuilder.append("x.click();");
            js.executeScript(stringBuilder.toString());
    
    2) 要验证页面内容,您可以在fb页面上找到多个web元素,只需验证页面上是否存在该元素:

    input.findElements(By.xpath("//xpath")).size() > 0
    //or by css selector: 
    input.findElements(By.cssSeector("html>...blablabla...")).size() > 0
    driver.findElement(By.cssSelector("html>...blablabla...")).isDisplayed()
    
    或者使用fluent wait等待fb登录页面上出现besic元素:

     public WebElement fluentWait(final By locator){
            Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(30, TimeUnit.SECONDS)
                    .pollingEvery(5, TimeUnit.SECONDS)
                    .ignoring(NoSuchElementException.class);
    
            WebElement foo = wait.until(
    new Function<WebDriver, WebElement>() {
                public WebElement apply(WebDriver driver) {
                            return driver.findElement(locator);
                    }
                    }
    );
                               return  foo;              }     ;
    
    publicwebelement fluentWait(最终由定位器确定){
    等待等待=新建FluentWait(驱动程序)
    .带超时(30,时间单位。秒)
    .轮询间隔(5,时间单位。秒)
    .忽略(NoSuchElementException.class);
    WebElement foo=wait.until(
    新函数(){
    公共WebElement应用(WebDriver){
    返回驱动程序findElement(定位器);
    }
    }
    );
    返回foo;};
    

    希望这对您有所帮助)

    您也可以使用此方法:

    new WebDriverWait(driver, 60).until(ExpectedConditions.presenceOfElementLocated(By.id("save")));
    
    //您的web对象ID为“保存”

    或者,您可以使用自定义方法。请参见下面的示例方法

     public static boolean waitForElement(By element, int timeOutInSeconds){
        boolean found = false;
        try{
            int counter=1;
            while(counter<timeOutInSeconds){
                if(driver.findElements(element).size()>0){
                    found = true;
                    break;
                }
                else{
                    Thread.sleep(1000);
                    counter++;
                }
            }
            return found;
        }
        catch(Exception e){
            e.printStackTrace();
            return found;
        }
    }
    
    公共静态布尔waitForElement(按元素,int-timeOutInSeconds){
    布尔值=false;
    试一试{
    int计数器=1;
    while(计数器0){
    发现=真;
    打破
    }
    否则{
    睡眠(1000);
    计数器++;
    }
    }
    发现退货;
    }
    捕获(例外e){
    e、 printStackTrace();
    发现退货;
    }
    }
    
    您自己尝试过任何代码吗?如果是,请邮寄。