简体   繁体   English

如何使用带有log4j的页面对象模型中的getText进行打印?

[英]How do I print using getText from a page object model with log4j?

I am coding some automated tests using the Selenium Webdriver in Netbeans. 我正在使用Netbeans中的Selenium Webdriver编写一些自动化测试。 I have a pretty simple test, and I am implementing page objects. 我有一个非常简单的测试,并且正在实现页面对象。 My trouble is here: 我的麻烦在这里:

  • I previously had this code: 以前有以下代码:

     //Click the Timing Parts subcategory WebElement PartSubcategory = driver.findElement(By.xpath("//label[contains(.,'Timing Parts & Camshafts')]")); PartSubcategory.click(); logger.info("Found subcategory: "+PartSubcategory.getText()); 
  • And, after implementing page object model, it looks like this. 并且,在实现页面对象模型之后,它看起来像这样。

Page object: 页面对象:

    public class findPartSubcategory {
    private static WebElement element = null;

    //Click the Timing Parts subcategory
    public static WebElement PartSubcategory(WebDriver driver)
            {
        element = driver.findElement(By.xpath("//label[contains(.,'Timing Parts & Camshafts')]"));
        return element;
            }        
     }

Test code: 测试代码:

    //Click the Timing Parts subcategory
    findPartSubcategory.PartSubcategory(driver).click();
    logger.info("Found subcategory: "+findPartCategory.getText());

So, the compile error is in getText(), "Cannot find symbol method getText()". 因此,编译错误在getText()中,“找不到符号方法getText()”。 I am guessing it's because I am not printing out a properly declared variable, but a page object class. 我猜这是因为我没有打印出正确声明的变量,而是页面对象类。

So how do I get to print what it found for that page object? 那么,如何打印针对该页面对象找到的内容? Yes, I am using log4j ver. 是的,我正在使用log4j ver。 1 1个

Thanks! 谢谢!

You have a compilation error. 您有编译错误。 Trying to reference findPartCategory.PartCategory which does not exist. 尝试引用不存在的findPartCategory.PartCategory

Change 更改

findPartCategory.PartCategory(driver).click();

To

findPartCategory.PartSubcategory(driver).click();

You are also trying to call getText() on your findPartSubcategory class and I assume you want to call that on the WebElement. 您还尝试在findPartSubcategory类上调用getText() ,我假设您想在WebElement上调用它。

 WebElement element = findPartSubcategory.PartSubcategory(driver); element.click(); logger.info("Found subcategory: "+ element.getText()); 

That should achieve the same functionality as your old code. 那应该实现与旧代码相同的功能。

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

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