简体   繁体   中英

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. 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()". 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. 1

Thanks!

You have a compilation error. Trying to reference findPartCategory.PartCategory which does not exist.

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.

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

That should achieve the same functionality as your old code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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