Java automation selenium convert to Pom or factory

Viewed 48

I need help with creating Java automation with Selenium Webdriver to Pom or Pomm factory. I've read how to create pom without any success. Please help. I need help on how to create java automation in pom. strong textHow to conver it?

String baseUrl = "https:amazon.com/";
WebDriver driver;
NavigationPom navigationPom;

private final boolean useFirefoxbrowser = false;

@BeforeClass
public static void setupClass() {
    WebDriverManager.chromedriver().setup();
    WebDriverManager.firefoxdriver().setup();

}

@Before
public void setUp() {

    if (useFirefoxbrowser == false) {
        FirefoxOptions firefoxOptions = new FirefoxOptions();
        firefoxOptions.addArguments("--width=1240", "--height=720");
        driver = new FirefoxDriver(firefoxOptions);
    } else {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--window-size=1920,1080");
        driver = new ChromeDriver(chromeOptions);
    }
}

@Test
public void MacbookTest1() {
    driver.get(baseUrl);
    driver.manage().window().maximize();
    driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']")).click();
    driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']")).sendKeys("macbook");
    driver.findElement(By.xpath("//input[@id='nav-search-submit-button']")).click();
    driver.findElement(By.xpath("//input[@id='nav-search-submit-button']")).click();
    driver.findElement(By.xpath("//li[@id='p_89/Lenovo']/span/a/div/label/i")).click();
    //Checkboxes
    boolean enabled = driver.findElement(By.xpath("//li[@id='p_89/Lenovo']/span/a/div/label/i")).isEnabled();



    
        
2 Answers

Start from creating a simple implementation:

How the Page class might look:

class Page {

    private WebDriver driver;

    public Page(WebDriver driver) {
        this.driver.driver = driver;
    }

    //Define locators
    
    //Keep elements as By on the top of the class
    private By someElement1 = By.xpath("...");
    private By someElement2 = By.xpath("...");


    //Define page methods
    
    // Examples:
    // public void clickSomeElement()
    // public void fillSomeInput()
    public void doSomeAction() {
        WebElement element = driver.findElement(someElement1);
        // do something with the element
        // optionaly wait for something after action
    }

    // Examples:
    // public boolean isPageLoaded()
    // public boolean isSomeElementDisplayed()
    // public String getSomeElementText()
    // public int getSomeElementsCount()
    public Object getSomeData() {
        WebElement element = driver.findElement(someElement2);
        // do something with the element and return
    }

}

Use pages in your tests, do not work with WebDriver directly.

@Test
public void someTest() {
    Page page = new Page(driver);
    page.doStep1();
    page.doStep2();
    assertEquals(page.getSomeData(), "Some expected result", "Optional Error message");
}

And..

Start from creating a test scenario code, create test steps, which you like to have (methods, which will be underlined in IDE as non-existing), and think about the data, which you like to check.

Then just implement all the non-existing methods you need in Page classes.

Maybe it sounds complicated, but try to start from simple test scenarios and it should become more clear after some practice.

I had the same issue. If this is a Java project, with Eclipse and Intellij IDEA, you can right click the project and select Convert to Maven. From there, it's fine adjustments (versions) in the POM file.

Related