Reputation: 71
Ok, so I am using POM
to create a kind of framework in Selenium/Maven
. The project has three 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
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.
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
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
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