1

Selenium Java inheritance want to reuse xpath in another class that registration forms has same button id like code below. I want reuse these three buttons in any class these buttons are

driver.findElementById("checkbox2").click();
driver.findElementById("checkbox1").click();
driver.findElementById("js-register").click();

public class Landindpage{ public void fill () {    
    System.setProperty("webdriver.chrome.driver","E:\\workspace\\chromeDriver\\chromeDriver.exe");
    ChromeDriver driver = new ChromeDriver();
    driver.get("http://qa-0.ls.vu/v2/landing-page/roche");
     driver.findElement(By.xpath("/html/body/nav/div/div[2]/ul/li[4]/a")).click();
        driver.findElement(By.xpath("/html/body/nav/div/div[2]/ul/li[4]/div/div[2]/a[1]")).click();
        driver.findElement(By.xpath("/html/body/nav/div/div[2]/ul/li[3]/a")).click();
        driver.findElement(By.xpath("/html/body/div[8]/div/div[3]/div/div/div/form/div[4]/div[1]/input")).sendKeys("Emmaname");
        driver.findElement(By.xpath("/html/body/div[8]/div/div[3]/div/div/div/form/div[4]/div[2]/input")).sendKeys("Mohaname");
        /*for(int count=1; count < 10; i++){
            driver.findElement(By.xpath("/html/body/div[8]/div/div[3]/div/div/div/form/div[5]/div[1]/input")).sendKeys("testing" + i + "@gmail.com");
            // Rest of the script
        }*/

        driver.findElement(By.xpath("/html/body/div[8]/div/div[3]/div/div/div/form/div[5]/div[2]/input")).sendKeys("0221-1234567");
        driver.findElement(By.xpath("/html/body/div[8]/div/div[3]/div/div/div/form/div[6]/div/select")).click();
        driver.findElement(By.xpath("/html/body/div[8]/div/div[3]/div/div/div/form/div[6]/div/select/option[3]")).click();
        driver.findElement(By.xpath("/html/body/div[8]/div/div[3]/div/div/div/form/div[7]/div/select")).click();    
        driver.findElement(By.xpath("/html/body/div[8]/div/div[3]/div/div/div/form/div[7]/div/select/option[4]")).click();
        driver.findElementById("checkbox2").click();
        driver.findElementById("checkbox1").click();
        driver.findElementById("js-register").click();
}

public  void googlefill (){ 

    driver.get("http://qa-0.ls.vu/v2/landing-page/google");
    driver.findElement(By.xpath("/html/body/div[3]/div/div/div/p[2]/a/small")).click();
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[3]/div[1]/input")).sendKeys("Emmaname");
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[3]/div[2]/input")).sendKeys("Mohaname");
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[4]/div/input")).sendKeys("aali3005@outlook.com");
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[5]/div")).click();
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[5]/div/select/option[2]")).click();      
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[6]/div[1]")).click();
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[6]/div[1]/select/option[4]")).click();
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[6]/div[2]")).click();
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[6]/div[2]/select/option[2]")).click();
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[7]/div/input")).sendKeys("0221-1234567");
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[8]/div")).click();
    driver.findElement(By.xpath("/html/body/div[9]/div/div[2]/div/div/div/form/div[8]/div/select/option[5]")).click();
    /*driver.findElementById("checkbox2").click();
    driver.findElementById("checkbox1").click();
    driver.findElementById("js-register").click();*/
}

there are two child classes of landing page

package Database;


public class Rochee   extends Landindpage {

    public static void main(String[]args)  {
        Rochee obj=new Rochee();
        obj.fill();
    }
}

package Database;

public class google extends Landindpage {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","E:\\workspace\\chromeDriver\\chromeDriver.exe");
        google bo1= new google();

        bo1.googlefill();
1

It is adviced to avoid inheritance as a means of code-reuse, also read https://softwareengineering.stackexchange.com/questions/75189/why-avoid-java-inheritance-extends

I suggest you have a look at the PageObject Model to remove code duplication from your tests. Read http://martinfowler.com/bliki/PageObject.html and http://artoftesting.com/automationTesting/pageObjectModel.html

If you have code that is shared between different pages you could create a central class which you instantiate from within the page classes.

If you do not want to use page object model I would have a look in making a static class for your shared references.

public class Buttons {
    public static string first = "//path";
    public static string second = "//path";
}

now you can access them from anywhere by calling

Buttons.first;
Buttons.second;

Also read:

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.