Java / SeleniumWebDriver:焦点不要跳到Else如果阻止警告消息在网页上匹配

时间:2018-01-24 07:27:21

标签: java selenium selenium-webdriver cucumber

我在网页上工作,当输入无效的用户名或密码时会显示警告消息。输入无效详细信息后,显示的消息将被更改,并表示用户被阻止。我试图在try / catch块中捕获这些不同的消息。但是,根据我正在验证的警告消息,我的代码不会将焦点转移到其他地方。

代码:

driver.findElement(By.name("submitted")).click();

    try {           
             if (new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "No Password Found for"))){

            String text = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");
            System.out.println(text);

            if(text.contentEquals("No Password Found for")){
        driver.navigate().refresh();

                Assert.fail("Unable to login to application");
            }
            }

             else if(new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "Your Online/Mobile Banking User ID has been blocked. Please go to “Forgot Password” option to unblock it."))){

String retext = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");

            System.out.println(retext);

            if(retext.contentEquals("Your Online/Mobile Banking User ID has been blocked. Please go to “Forgot Password” option to unblock it.")) {
                driver.navigate().refresh();

                Assert.fail("Unable to login to application");
            }


        }       

             else if (new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "Time Out"))){
            String timeouttext = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");

            if(timeouttext.contentEquals("You have specified an invalid User Name or Password. Please check and try again")){
                driver.navigate().refresh();

                                    Assert.fail("Unable to login to application");      }


        }

1 个答案:

答案 0 :(得分:3)

由于你的尝试包括所有条件,我假设当它找不到第一个元素并抛出异常时,它会跳过所有其余的try。

做这样的事情:

try {
if (new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "No Password Found for")))
             {
                String text = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");
                System.out.println(text);
                if(text.contentEquals("No Password Found for"))
                {
                    driver.navigate().refresh();
                    Assert.fail("Unable to login to application");
                }
            }
}
catch(Exception){}
try{
if(new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "Your Online/Mobile Banking User ID has been blocked. Please go to “Forgot Password” option to unblock it.")))
             {
                String retext = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");
                System.out.println(retext);
                if(retext.contentEquals("Your Online/Mobile Banking User ID has been blocked. Please go to “Forgot Password” option to unblock it.")) 
                {
                    driver.navigate().refresh();
                    Assert.fail("Unable to login to application");
                }
            }
catch(Exception){}

等等。

尽管如此,性能从这个元素获取文本并进行评估会更好,而不是多次尝试查找元素。