user5400828
user5400828

Reputation: 71

How do I create a driver.get function in Selenium that will be called from another class?

Ok, so I am using POM to create a kind of framework in Selenium/Maven. The project has three packages

  1. Pages(which include the different webpages I will navigate through)
  2. Test(which will call functions and execute) and
  3. Util(containing a Library class with functions that I want to call from classes in the Test and Pages packages)

Here is my code from the Library class:

package util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Lib {

//Question is regarding the method below
public static WebElement get(WebDriver driver, String url) {
    WebElement link = driver.get(url);
    return link;
}

public static WebElement findelement(WebDriver driver, String xpath) {
    WebElement e = driver.findElement(By.xpath(xpath));
    return e;
}

public static void sleep(WebDriver driver, int sec) throws InterruptedException {
    Thread.sleep(sec*1000); 
}
}

I want to be able to call the get method(first one) from the Lib class in the test class. Here is my code for the test class:

package test;

import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import util.Lib;

public class Test extends Lib{

static WebDriver driver = new ChromeDriver();

@Before
public void before() {
    System.out.println("Before Class starts here");

    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\TQ\\sel\\chromedriver_win32\\chromedriver.exe");

    driver = new ChromeDriver();

    Lib l = new Lib();
    l.get(driver, "https://www.google.com/");


}
}

I know I could easily do a driver.get(url) in the test class but I want to know why its not working like it is for the other functions in my lib class.

This is the error message I'm getting "cannot convert from void to WebElement".

Thanks in advance.

Upvotes: 0

Views: 3552

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193308

The error says it all :

"cannot convert from void to WebElement"

You have written the method public static WebElement get(WebDriver driver, String url) {}. But get() doesn't returns anything and is defined as :

get(java.lang.String url)
Load a new web page in the current browser window.

Return Type :
  void

Hence when you invoke WebElement link = driver.get(url);, the return type doesn't matches the function call and the error is thrown.

Solution

The solution would be to make the return as void. As the driver opens the URL you can easily interact with the Web Browser instance through the WebDriver instance i.e. driver as follows :

public static void get(WebDriver driver, String url) {
    driver.get(url);
}

Upvotes: 1

Pavel Stryhelski
Pavel Stryhelski

Reputation: 41

This method is void. It should look like:

//Question is regarding the method below
public static void get(WebDriver driver, String url) {
      driver.get(url);
}

That`s all. You need not to return something is this case.

Upvotes: 1

Manmohan_singh
Manmohan_singh

Reputation: 1804

You have to pass the reference object of webdriver from test class to lib. Remove the extends Lib from test class. Create a constructor in lib class for initialising the reference object. The Library class should look something like this.

package util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Lib {
WebDriver driver;
public Lib(WebDriver driver){
     this.driver = driver;
}
//Question is regarding the method below
public static WebElement get(String url) {
    WebElement link = driver.get(url);
    return link;
}

public static WebElement findelement(String xpath) {
    WebElement e = driver.findElement(By.xpath(xpath));
    return e;
}

public static void sleep(int sec) throws InterruptedException {
    Thread.sleep(sec*1000); 
}
}

Now , When you create the Lib object using new operator , you have to pass driver instance. The constructor will pass on the driver reference to local driver variable, which will allow Test class to use Lib class methods.

Lib l = new Lib(driver);
l.get("https://www.google.com/");

The coded way by which you passed on driver instance to method parameters will lead to null pointer exception.

Upvotes: 1

Related Questions