Written the Test Script in TestNG framework where the Open browser activity is happening in "baseClass" under @BeforeTest
annotation and the Test Script is written in Test Packages under as @Test
Annotation. IN the test Script I am extending the Base Class so that my @beforetest
(opening the browser) will work first and then test case functionality should execute.
But at TestScript I am getting NULLPointerException.
@BeforeTest
methods are returning the webdriver object and I thought it will work at @Test
methods but this is failing
Can anyone guide me here how to proceed further?
public class BaseClass {
public WebDriver driver;
@BeforeTest
public WebDriver openBrowser()
{
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"/Drivers/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(120));
driver.manage().window().maximize();
driver.get("https://app.vwo.com/#/analyze/heatmap/129/reports?token=eyJhY2NvdW50X2lkIjo2LCJleHBlc%20mltZW50X2lkIjoxMjksImNyZWF0ZWRfb24iOjE1MDc3ODk0ODcsInR5cGUiOiJjYW1wYWlnbiIsI%20nZlcnNpb24iOjEsImhhc2giOiJiMzlmMTQ4MWE0ZDMyN2Q4MDllNTM1YzVlNWFjOGVlMCJ9");
return driver;
}
public class Assignment extends BaseClass {
WebDriver driver;
CommonMethods common = new CommonMethods();
@Test
public void executeTest()
{
WebElement heatMap_locator=driver.findElement(By.xpath(common.readPropertyFile("viewheatmap_xpath")));
common.clickJavaScriptExecutor(driver, heatMap_locator);
}
2 Answers 2
If you extend a class having driver
field where that field is initialized do not add filed of the same name to child class. Doing that you're hiding a field of base class.
Here is the example:
Your base:
public class TestBase {
public Object object;
@BeforeTest
public void doBefore(){
object = new Object();
}
}
Your test that WILL fail (a.k.a BAD TEST):
public class TestChild extends TestBase{
Object object;
@Test
public void doTest(){
Assert.assertNotNull(object);
}
}
Your test that WON'T fail (a.k.a GOOD TEST):
public class TestChild extends TestBase{
@Test
public void doTest(){
Assert.assertNotNull(object);
}
}
-
1Yes I have fixed in this way only and it's working. Thanks for responseRahil Kumar– Rahil Kumar2021年06月10日 16:02:49 +00:00Commented Jun 10, 2021 at 16:02
Create a base class where you write a method of initializing. then you an extend that class to your main test case, then you can initialize that method of initializing in @beforeTest
public static void initConfiguration(){
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir") + "\\src\\test\\resources\\executables\\chromedriver.exe");
driver = new ChromeDriver(options);
driver.get(baseurl);
driver.manage().window().maximize();
}
above code, you can write in base class then extend that class to your testcase class.
-
I have done the same way. I missed to add test class. Please review onceRahil Kumar– Rahil Kumar2021年06月10日 10:40:41 +00:00Commented Jun 10, 2021 at 10:40
-
don't use "@beforetest" annotation in your base class. extent base class in your test class and just call the method which is doing launching and navigation in your "@test" annotationSubhash Bohra– Subhash Bohra2021年06月10日 12:28:13 +00:00Commented Jun 10, 2021 at 12:28
Explore related questions
See similar questions with these tags.
NullPointerException
stands for? Have you looked at your stacktrace? See this answer to see how to read stacktraces and understand exceptions: sqa.stackexchange.com/a/51649/1933