当我在网页上看到“登录”按钮时,我需要单击“登录”按钮,否则无需采取任何措施

问题描述 投票:0回答:2

我的要求:

[当我在网页上看到“登录”按钮时,我需要单击“登录”按钮。否则,无需采取任何措施(因为我已经登录)。

因此,我使用@step注释(Allure Framework)编写了如下的登录功能。

@Step("Click on Login and verify whether User is in Login Page.")
public void goTo_Us_Login(String expectedText) {        
boolean elementPresent = isElementPresent(By.xpath((loginXpath)));
if (elementPresent) {
click(By.xpath(loginXpath));
//Assert.assertEquals(readText(By.xpath(v_LoginXpath)), expectedText, "User is NOT in " + getCallingMethodName().replace("goToUa", "").replace("Page", " Page.\n\n"));
click(By.xpath(continueXpath));
//Assert.assertEquals(readText(By.xpath(v_ContinueXpath)), expectedText, "User is NOT in " + getCallingMethodName().replace("goToUa", "").replace("Page", " Page.\n\n"));
writeText(By.xpath(emailAddressXpath),"[email protected]");
writeText(By.xpath(passwordXpath),"test$");
click(By.xpath(signinXpath));
Assert.assertEquals(readText(By.xpath(v_DashboardXpath)), expectedText, "User is NOT in " + getCallingMethodName().replace("goToUa", "").replace("Page", " Page.\n\n"));            }       
}   

现在,我从@Test调用登录功能,如下所示。

@Test(description = "Navigation to settings_Preferences - pioneer.com/us", dependsOnMethods = {"verify_GoToUsHomePageMethod"})
@Description("Verify whether settings_Preferences are working as expected")
@Severity(SeverityLevel.CRITICAL)
@Parameters({ "baseURL" })
public void settings_Preferences_Method(Method method, String baseURL) {
     SoftAssert assertions = new SoftAssert();
     String expTxtOn_Us_Login_Page = td.getProperty("expTxtOn_Us_Login_Page");
     String expTxtOn_Us_Settings_Page = td.getProperty("expTxtOn_Us_Settings_Page");
     Log.info(method.getName() + " test is started.");
     UsHomePage usHomePage = new UsHomePage(prop, driver, wait);
     UsDashboardPage usDashboardPage = new UsDashboardPage(prop, driver, wait);
     usHomePage.goTo_Us_PioneerHomePage(baseURL);

     **usHomePage.goTo_Us_Login(expTxtOn_Us_Login_Page);**

     usDashboardPage.goTo_Us_Settings(expTxtOn_Us_Settings_Page);
     usDashboardPage.verify_Us_Settings_Preferences("true",assertions);
     usHomePage.goTo_Us_Signout(assertions);
     assertions.assertAll();
}

这里的问题是:

[elementPresent = FALSE(即我已经登录),然后

  1. 抛出以下消息

    预期条件失败:等待元素的存在:By.xpath:// li [@ class ='未登录身份验证'] // a [contains(text(),'LOG IN')]](已尝试持续100秒钟,间隔为500毫秒)`和

  2. 以下代码未执行

    usDashboardPage.goTo_Us_Settings(expTxtOn_Us_Settings_Page);
    usDashboardPage.verify_Us_Settings_Preferences("true",assertions);
    usHomePage.goTo_Us_Signout(assertions);     
    assertions.assertAll();
    

我期待以下。

尽管elementPresent = FALSE(即我已经登录),但我需要执行以下代码。

 usDashboardPage.goTo_Us_Settings(expTxtOn_Us_Settings_Page);
 usDashboardPage.verify_Us_Settings_Preferences("true",assertions);
 usHomePage.goTo_Us_Signout(assertions);
 assertions.assertAll();
selenium selenium-webdriver allure
2个回答
0
投票

您没有发布isElementPresent()方法,所以我假设这就是问题所在。我猜想您在此处使用了driver.findElement()调用(请注意单数),并且在找不到元素时抛出该异常。而是使用.findElements()(复数)并检查一个空列表。

public static boolean isElementPresent(By locator)
{
    return !driver.findElements(locator).isEmpty();
}

然后您将以类似方式使用它

@Step("Click on Login and verify whether User is in Login Page.")
public void goTo_Us_Login(String expectedText) {        
    if (isElementPresent(By.xpath(loginXpath))) {
        click(By.xpath(loginXpath));
        click(By.xpath(continueXpath));
        writeText(By.xpath(emailAddressXpath),"[email protected]");
        writeText(By.xpath(passwordXpath),"test$");
        click(By.xpath(signinXpath));
        Assert.assertEquals(readText(By.xpath(v_DashboardXpath)), expectedText, "User is NOT in " + getCallingMethodName().replace("goToUa", "").replace("Page", " Page.\n\n"));
    }
} 

0
投票

检查是否登录的逻辑:

  1. 等待登录主页上的任何元素]之一的状态
  2. 如果登录
  3. 存在,请登录
    new WebDriverWait(driver, 10).until(d -> d.findElements(By.xpath(loginXpath)).size() > 0 || 
            d.findElements(By.xpath(elementOnMainPageXpath)).size() > 0);
    if (driver.findElements(By.xpath(loginXpath)).size() > 0)  {
        // login here
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.