简体   繁体   中英

How to use an instance of a class in an another class in selenium

Login.java

public class Login_Details {
    public static WebDriver dr;
    public void verifyLogin() throws Exception {
        dr= new FirefoxDriver();
        dr.get("http://www.nexterp.in:9992/NextERPQA/nerp/login");
        FileInputStream fi= new FileInputStream("C:\\NexterpProject\\Next_Testdata\\src\\Testdata.xls");
        Workbook wb= Workbook.getWorkbook(fi);
        Sheet s= wb.getSheet(0);
        dr.findElement(By.id("userName")).sendKeys(s.getCell(0,1).getContents());
        dr.findElement(By.id("password")).sendKeys(s.getCell(1,1).getContents());
        dr.findElement(By.xpath(".//*[@id='loginForm']/a")).click();
        Thread.sleep(3000);
        System.out.println("Thank you for Logging in");
    }

and Logout.java

public class Logout{
public static WebDriver dr;

public void comesOut() throws Exception{
    Thread.sleep(1000);
    dr.findElement(By.xpath("html/body/div[1]/div/div/div/div[2]/div/a[2]")).click();
    System.out.println("Thank you for clicking on Logout");
    dr.quit();
    }
}

in the above two programs there am creating an instance for webdriver dr; my question is instead of creating every time a new instance for new class, tell me how can i use that dr in my programs ?

You can move initialization of static object from method to variable declaration Eg

 public static WebDriver dr = new FirefoxDriver();

And then reuse it every where like

Login_Details.dr

使驱动程序处于初始化阶段。

public static WebDriver dr = new FirefoxDriver();

You can extend the "Login.java" class in "Logout.java" like below, (and also do import the Login class in Logout class too ):

public class Logout extends Login

Then you can directly use dr in "Logout.java"

Create a BaseClass which contains driver initialization with driver as static. Extend BaseClass to each and every class and reuse driver object.

public class BaseClass{
    static WebDriver driver;
    .
    .
    .
}

in Login_Details

public class Login_Details extends BaseClass{
    public void verifyLogin() throws Exception {
        driver= new FirefoxDriver();
        driver.get("http://www.nexterp.in:9992/NextERPQA/nerp/login");
        <your code>
    }
}

and in Logout.java

public class Logout extends BaseClass{
    public void comesOut() throws Exception{
        driver.findElement(By.xpath("html/body...")).click();
        <your code>
    }
}

And if you don't want to make your driver static, its better you follow PageObject design pattern. Using that u can provide your code without extending BaseClass.

in Login_Details

public class Login_Details{
    WebDriver driver = null;
    public Logout verifyLogin() throws Exception {
        driver= new FirefoxDriver();
        driver.get("http://www.nexterp.in:9992/NextERPQA/nerp/login");
        <your code>
        return new Logout(driver);
    }
}

and in Logout.java

public class Logout{
    WebDriver driver;
    public Logout(WebDriver driver) {
        this.driver = driver;
    }
    public void comesOut() throws Exception{
        driver.findElement(By.xpath("html/body...")).click();
        <your code>
    }
}

So your test method would look like:

@Test
public void test1() {
    Login login = new Login();
    Logout logout = login.doLogin();
    logout.doLogout();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM