简体   繁体   English

如果条件在 selenium 中不起作用,则 Boolean 表达式

[英]Boolean expression in If condition is not working in selenium

I am trying to create a scenario in automation in which it enters Login credentials and clicks on Sign In button and after that it searches for an element.我正在尝试在自动化中创建一个场景,在该场景中它输入登录凭据并单击登录按钮,然后搜索一个元素。 If element is displayed then it prints Valid Credentials and if element is not displayed then it does some other operation but code doesn't work after clicking on Sign In button.如果显示元素,则打印有效凭据,如果未显示元素,则执行其他操作,但单击登录按钮后代码不起作用。 Here I have created a method for it.在这里,我为它创建了一个方法。

public void toCheckLoginVerification() throws InterruptedException,MalformedURLException {
    clickOnSignInbutton();
    enterEmailId("s243535@yopmail.com");
    enterPassword("Test@123");
    clickOnSignInButtonOnSignInScreen(); //code not working after this.
    boolean x = driver.findElement(By.xpath("//android.view.View[@content-desc='Trips']")).isDisplayed();
    if(x==true) {
        System.out.println("Valid Credentials");
    } else {
        System.out.println("Invalid Credentials");
        clearEmailid();
        clearPassword();
        enterEmailId("s2@yopmail.com");
        enterPassword("Test@123");
        clickOnSignInButtonOnSignInScreen();
        clickOnProfileButtonOnHomeScreen();
    }
}

I have created the above methods in other class but calling them here.我在其他 class 中创建了上述方法,但在这里调用它们。 To be more specific I am using Appium.更具体地说,我正在使用 Appium。

You should use explicit wait instead of implicit wait.您应该使用显式等待而不是隐式等待。
With your code selenium finds the element at the moment it is just created but still not fully loaded / rendered.使用您的代码 selenium 在它刚刚创建但仍未完全加载/渲染的那一刻找到元素。 So when your driver.findElement(By.xpath("//android.view.View[@content-desc='Trips']")) returns the element it is still not displayed / visible.因此,当您的driver.findElement(By.xpath("//android.view.View[@content-desc='Trips']"))返回它仍然不显示/可见的元素时。
While you should wait until the element is found visible.虽然您应该等到发现元素可见。
So, you can use the following method:因此,您可以使用以下方法:

public boolean waitForElementToBeVisible(String xpath, int delay) {
        wait = new WebDriverWait(driver, delay);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(element));
            return true;
        }catch (Exception e){
            return false;
        }
    }

Now you can say:现在你可以说:

if(waitForElementToBeVisible("//android.view.View[@content-desc='Trips']",10)){
    System.out.println("Valid Credentials");
} else{
    System.out.println("Invalid Credentials");
    clearEmailid();
    clearPassword();
    enterEmailId("s2@yopmail.com");
    enterPassword("Test@123");
    clickOnSignInButtonOnSignInScreen();
    clickOnProfileButtonOnHomeScreen();
}

You are checking whether the element is displayed immediately.. Instead you should use implicit or explicit wait mechanism of selenium您正在检查元素是否立即显示。相反,您应该使用 selenium 的隐式或显式等待机制

'Statement' inside if block will be executed if the Boolean expression (Condition) is true.如果 Boolean 表达式(条件)为真,则将执行 if 块中的“语句”。 If the Boolean expression is false then, statements inside if block will not be executed and it executes the rest of code after if block.如果 Boolean 表达式为假,则 if 块中的语句将不会被执行,它会执行 if 块之后的代码 rest。

READ THIS 读这个

if(waitForElementToBeVisible("//android.view.View[@content-desc='Trips']",10)){ System.out.println("Valid Credentials"); } else{ System.out.println("Invalid Credentials"); clearEmailid(); clearPassword(); enterEmailId("s2@yopmail.com"); enterPassword("Test@123"); clickOnSignInButtonOnSignInScreen(); clickOnProfileButtonOnHomeScreen(); } public boolean waitForElementToBeVisible(String xpath, int delay) { wait = new WebDriverWait(driver, delay); try { wait.until(ExpectedConditions.visibilityOfElementLocated(element)); return true; }catch (Throwable t){ return false; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM