Question

// I want to use my own custom elements instead of selenium defined web elements //like webElement and WebDriver etc. So,I have created following custom wrapper classes :-

//custom webdriver wrapper
package org.rsf.wrapper.driver;

import org.openqa.selenium.WebDriver;

public interface RsfWebDriver extends WebDriver{

}



//custom webelement wrapper
package org.rsf.wrapper.element;

import org.openqa.selenium.WebElement;

public interface RsfElement extends WebElement{


}

// Now, I am calling my wrapper classes in below pageObject code .But it gives error.Pls //help me:-

package org.rsf.pages;

import java.util.concurrent.TimeUnit;


import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;
import org.rsf.bean.EnterQuoteBean;
import org.rsf.utilities.Utility;
import org.rsf.wrapper.driver.RsfWebDriver;
import org.rsf.wrapper.element.RsfElement;
import org.testng.Assert;


//pageObject 
public class EnterQuotePage extends PageBase {


    public EnterQuotePage(RsfWebDriver driver) {

        super(driver);
        super.driver=driver;

    }

    //creating my custom wrapper object
    private RsfElement element;
    private static Logger log=Logger.getLogger(EnterQuotePage.class);

     Utility utilities = new Utility();
     EnterQuoteBean eqb= new EnterQuoteBean();



     public void open(String url) {
            System.out.println("Inside get url");
            System.out.println("url is : " +url);
            System.out.println(" PB : Driver value :-" + driver);
            Assert.assertNotNull(this.driver);
            this.driver.get(url);
            System.out.println("After get url");
        }




    public void clickHit()
    {
          super.driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

              //ERROR OCCURS IN BELOW TWO LINES [in element]
              element=super.driver.findElement(By.id("hit"));
          element.click();
    } 




    public String getQuoteID(){

        String quoteText =findMessage.getText();
        String quoteText1[] = quoteText.split("\\=");
        quoteText1[1]=quoteText1[1].trim();
        System.out.println("Generated Quote Id :- " + quoteText1[1]);
        return quoteText1[1];
    }



    public void checkSuccessMessage(){

        String successMessage = findMessage.getText();

        Assert.assertEquals(element.getText().contains(successMessage), eqb.getSuccessMessage());

    }


    }
Was it helpful?

Solution

To solve this problem do as below

Create a XML file with elements and add an XML parse class, you can find code online easily for XML parser. e.g.

  <Element name="ElementSource">
    <Sample>
        <ElementTagName by="Xpth,id etc..">LocaterValue</ElementTagName>
        <txtUserName by="id">username</txtUserName>
    </Sample>

Create a class which have static web page elements Same as in the XML file e.g.

public static final String txtUserName = "txtUserName";

You can have page wise classes or a single class. Also, you can directly mention your elements in this class without XML file.

Create a class that contains modules to read this XML file or Element Class to get Element Locater and its locate by like xpath,id etc.. you can store them into Variables.

Finally, Create a class Named ElementLocater having function to locate elements as per its locate by using Switch case. e.g.

    public WebElement locateElement(WebDriver driver, String Locater,
            String LocateBy) {
        try {
        switch (LocateBy.toLowerCase()) {
        case "xpath":
        return driver.findElement(By.xpath(Locater));
        case "id":
        return driver.findElement(By.id(Locater));
        ....
        //So on
        default:
        return null;
        }
        } 
        catch (Exception e) 
        {
            //Write to log or custom error message
        }
        return null;
        }

You can create more functions in this class like isElementPreset etc...

By doing this you don't have to go into source code or in Tests every time any element HTML chages or Code update need to be done. Change any one function or variable will do :)

OTHER TIPS

You need to catch a NoSuchElementException:

try
{
    element=super.driver.findElement(By.id("hit"));
    element.click();
}
catch (NoSuchElementException e)
{
    // Do whatever you were planning to do if the element was not found
    // (you can try again for example)
}

Alternatively, you can do the following (will "save" you the exception handling):

    List<WebElement> elements = super.driver.findElements(By.id("hit"));
    if (elements.size() > 0)
        elements.get(0).click();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top