0

配置 :

硒:2.53.1

爪哇:7

Eclipse IDE:火星

我正在使用 POM 框架并为此使用 PageFactory 设计模式。我有以下主页代码:

public class RCONHomePage 
{
    @FindBy(css =".ng-scope>a span[translate='login.register']")
    public WebElement loginLink;
    
    @FindBy(xpath ="//a//span[text()='DASHBOARD']")
    public List<WebElement> dashboardLink;
    
    @FindBy(name = "number")
    public WebElement globalSearchMobileNumbertextBox;
    
    @FindBy(xpath = "//button[@class='btn rc-bg-border']")
    public WebElement globalSearchButton;
    
    @FindBy(css = "p.page-title.ng-scope >span")
    public WebElement globalSearchResult;
    
    
    WebDriver driver;
    
    public RCONHomePage(WebDriver driver)
    {
    
        PageFactory.initElements(new AjaxElementLocatorFactory(driver, 30), this);
        this.driver=driver;
    }
    
    public void clickOnLoginLink()
    {
        loginLink.click();
    }
    
    public void enterMobileNumberForGlobalSearch(String mobileNumber)   
    {
        globalSearchMobileNumbertextBox.clear();
        globalSearchMobileNumbertextBox.sendKeys(mobileNumber);
    }
    
    public void clickGlobalSearchButton()
    {
        globalSearchButton.click();
    }
    
    public String getGlobalSearchResult()
    {

        System.out.println(globalSearchResult.getText());
        return globalSearchResult.getText();

    }
}

我的用例是在全球搜索中输入有效和无效的手机号码,并验证结果天气该号码是否存在于本网站

问题是,如果手机号码无效(显示“找不到记录”),我的测试将显示正确的结果,但如果我输入有效的手机号码并验证文本,它仍然显示“找不到记录”。如果我手动找到有效数字的文本。该元素在 DOM 中不可用。

这些是场景的测试方法:

@Test
public void searchForNonExistContact() throws InterruptedException, IOException
{
        homepage = new RCONHomePage(driver);
    
        homepage.enterMobileNumberForGlobalSearch("9422307800");
        homepage.clickGlobalSearchButton();
        System.out.println(homepage.globalSearchResult.getText());
    }
    
}

@Test
public void searchForExistContact() throws InterruptedException, IOException
{
        homepage = new RCONHomePage(driver);
    
        homepage.enterMobileNumberForGlobalSearch("9422307801");
        homepage.clickGlobalSearchButton();
        System.out.println(homepage.globalSearchResult.getText());
    }
    
}

据我所知,如果我使用@CacheLookup该元素,它应该会发生。我不知道为什么会这样。有人可以帮我吗 ?

4

1 回答 1

1

我认为代码看起来不错,问题可能看起来像“测试在运行时失败但在调试时失败”。就我而言,我会采取更多步骤来弄清楚应该发生什么:

  1. 正确的文本可能会在“延迟后”出现,或者“找不到记录”是默认消息,“找到记录”需要一些时间。在这种情况下,我将隐式等待一秒钟并再次运行脚本。
  2. 我不会打印出文本,而是使用预期的文本调用 findEment。通过这样做,系统将等到超时来获取元素。

我希望这些步骤可以帮助弄清楚系统的行为,以便有更好的方法来测试场景。

就我而言,我使用以下功能等待测试:

 /***
 * An expectation for checking WebElement with given locator has text with a value as a part of it
 * @param locator - used to find the element
 * @param pattern - used as expected text matcher pattern
 * @param timeout
 * @return Boolean true when element has text value containing @value
 */
public boolean textMatches(By locator, Pattern pattern, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textMatches(locator, pattern));
    } catch (Exception e) {
        return false;
    }
}

/***
 * An expectation for checking WebElement with given locator has specific text
 * @param locator - used to find the element
 * @param value - used as expected text
 * @param timeout
 * @return Boolean true when element has text value equal to @value
 */
public boolean textToBe(By locator, String value, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textToBe(locator, value));
    } catch (Exception e) {
        return false;
    }
}

/***
 * An expectation for checking if the given text is present in the specified element.
 * @param element - the WebElement
 * @param text - to be present in the element
 * @param timeout
 * @return true once the element contains the given text
 */
public boolean textToBePresentInElement(WebElement element, String text, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textToBePresentInElement(element, text));
    } catch (Exception e) {
        return false;
    }
}

/***
 * An expectation for checking if the given text is present in the element that matches the given locator.
 * @param locator - used to find the element
 * @param text - to be present in the element found by the locator
 * @param timeout
 * @return true once the first element located by locator contains the given text
 */
public boolean textToBePresentInElementLocated(By locator, String text, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textToBePresentInElementLocated(locator, text));
    } catch (Exception e) {
        return false;
    }
}

/***
 * An expectation for checking if the given text is present in the specified elements value attribute.
 * @param locator - used to find the element
 * @param text - to be present in the value attribute of the element found by the locator
 * @param timeout
 * @return true once the value attribute of the first element located by locator contains the given text
 */
public boolean textToBePresentInElementValue(By locator, String text, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textToBePresentInElementValue(locator, text));
    } catch (Exception e) {
        return false;
    }
}

/***
 * An expectation for checking if the given text is present in the specified elements value attribute.
 * @param element - the WebElement
 * @param text - to be present in the element's value attribute
 * @param timeout
 * @return true once the element's value attribute contains the given text
 */
public boolean textToBePresentInElementValue(WebElement element, String text, int timeout) {
    try {
        return getWebDriverFluentWait(timeout)
                .until(ExpectedConditions.textToBePresentInElementValue(element, text));
    } catch (Exception e) {
        return false;
    }
}

private Wait<WebDriver> getWebDriverFluentWait(int timeout) {
    return new FluentWait<WebDriver>(driver)
            .withTimeout(timeout, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);
}
于 2017-09-18T08:50:36.690 回答