Create a Reusable method to Verify List Elements by Selenium WebDriver

735 views
Skip to first unread message

SeleniumMember

unread,
Oct 15, 2015, 4:13:02 PM10/15/15
to Selenium Users
Below is my test method

Class - TestMethods

    public CommonTestMethods getButtons(WebDriver driver) {
        String classLocator = "div.action-icon";
        String[] expectedButtons = { "Create", "Edit", "Delete", "Save", "Import"};  [ Note : How can I make this method reusable as "expectedButtons" list is different for different UI page]
        List<WebElement> elements = driver.findElements(By.cssSelector(classLocator));
        if (expectedButtons.length != elements.size()) {
            System.out.println("Actual and Expected # of buttons do not match");
        }

        for (int i = 0; i < expectedButtons.length; i++) {
            String buttonName = elements.get(i).getText();
            if (buttonName.equals(expectedButtons[i])) {
                System.out.println("Button name is : " + buttonName);
            } else {
                System.out.println("Unexpected Button name is : " + buttonName);
            }
        }

Class - TestClass
    @Test
    public void createQuery() {
        CommonTestMethods common = new CommonTestMethods ();
        common.login("username", "password");
        common.getButtons(driver);  [NOTE: The above testmethod "getButtons" will be used in every test TestClass similar to this. The only different in each test is the Array list "expectedButtons" which are button names ]
}

       

Kendall Schmidt

unread,
Oct 15, 2015, 7:00:41 PM10/15/15
to Selenium Users
You change your getButtons() method signature to take another argument which is the Array of expected buttons.
Message has been deleted

SeleniumMember

unread,
Oct 16, 2015, 9:09:25 AM10/16/15
to Selenium Users
Thank you for the reply. Here is what I did and I have another issue, maybe I am not understanding this....

public CommonTestMethods getButtons(WebDriver driver, String[] expectedButtons) {
        String classLocator = "div.action-icon";

    
        List<WebElement> elements = driver.findElements(By.cssSelector(classLocator));
        if (expectedButtons.length != elements.size()) {
            System.out.println("Actual and Expected # of buttons do not match");
        }

        for (int i = 0; i < expectedButtons.length; i++) {
            String buttonName = elements.get(i).getText();
            if (buttonName.equals(expectedButtons[i])) {
                System.out.println("Button name is : " + buttonName);
            } else {
                System.out.println("Unexpected Button name is : " + buttonName);
            }
        }

Class - TestClass
    @Test
    public void createQuery() {
        CommonTestMethods common = new CommonTestMethods ();
        String[] expectedButtons =  { "Create", "Edit", "Delete", "Save", "Import"};
        common.login("username", "password");
        common.getButtons(driver, expectedButtons);  [NOTE: Now my problem is, in each test, page to page, the buttons are different. So, I can't create multiple Array list for "expectedButtons"
                                                                                         For ex, expectedButtons2= {"Create" , "Edit"}, expectedButtons3 = {"Delete, "Save""} etc
}

********************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************************

MWQA

unread,
Oct 16, 2015, 9:28:36 AM10/16/15
to Selenium Users
I don't think your design is appropriate if your buttons are different on each page .

Why not just use a page object model approach and verify that each of the buttons exist on a page by page basis (i.e. a few lines of simple webdriver code)?

anemuday

unread,
Oct 16, 2015, 12:52:26 PM10/16/15
to Selenium Users
Hi,

Please find the below video will help your needs.

Uday
Reply all
Reply to author
Forward
0 new messages