0

I am new to selenium, And In my scenario I need to select a product for list of products in the page. And Product name to select is a test data. Now my query is,Can I create Object in Class level ( like Page Obj Model) with parameters?

By itemOne1 = By.xpath("//div[@text='VARIABLE']")  // **@ Beginning of the  class, Page Object Model**

Or do I need to create Object local to method?

By itemOne1 = By.className("product-title");   // @ Beginning of the class

In Method, to get the requested Item

for(WebElement we : itemOnes) {
            System.out.println(we.getText().equals(itemName));
            if(we.getText().equals(itemName)){
                we.findElement(By.tagName("a")).click();
            }
        }

Please clarify ..

  • I'm not getting you about Can I create Object in Class level. Do you want to use Page Object approach? – Alexey R. Aug 9 '18 at 11:08
  • You are correct, Page object.. how can I create such object in Page Object .. – SQA_LEARN Aug 9 '18 at 11:26
1

If I got you right you have the case like this:

enter image description here

If yes, then you can define all the items in the container in the way Selenium provides to define page objects and then iterate through the items from that container. Here is the sample PageWithSideNav class implementing the approach against https://www.w3schools.com/ page:

package ar.test;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.List;
import java.util.stream.Collectors;

public class PageWithSideNav {

    @FindBy(xpath = "//nav[@id='mySidenav']//a")
    List<WebElement> sideNavItems;

    public void clickItemWithTextJava8(String text) {
        List<WebElement> filteredList = sideNavItems.stream().filter(s ->
                s.getText().equals(text)
        ).collect(Collectors.toList());
        if (filteredList.size() != 1) {
            // Excpetion case. Handle.
        } else {
            filteredList.get(0).click();
        }
    }

    public void clickItemWithTextJavaPre8(String text) {
        for (WebElement element : sideNavItems) {
            if (element.getText().equals(text)) {
                element.click();
                return;
            }
        }
        // Exception case. Handle.
    }

}

It actually introduces two identical methods demonstrating how could the approach look like in Java8 and later (lambda style) and in pre-Java8. Below is the example on how to use this PageWithSideNav:

package ar.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;

public class Main {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "E:/Dev/WebDrivers/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.w3schools.com");

        PageWithSideNav page = PageFactory.initElements(driver, PageWithSideNav.class);

        page.clickItemWithTextJava8("Learn Colors");

        driver.navigate().back();

        page.clickItemWithTextJavaPre8("Learn Graphics");

        driver.close();

    }
}
0

Yes, you can create xpaths for elements having commonality in the syntax like text. You can get the list of all elements of that kind and then in a for loop compare the texts to match the exact element followed by the operation on that element. This is a good example of bringing and working with dynamic content in a page.

For example, you have list of usernames in a page with the usernames that are recently added to appear no top. These scenarios can be gracefully handled by the above mechanism.

Some code sample:

    public void click_to_see_detailed_view_from_customer_name(String _cust_name){
        for (WebElement w : get_list_customer_slice_name()){
            if (w.getText().equals(_cust_name))
                w.click(); break;
        }
    }

    public List<WebElement> get_list_customer_slice_name(){
        return driver.findElements(_lnk_customer_slice_name) ;
    }

_lnk_customer_slice_name : Should have xpath to get all elements of similar kind that you are looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.