I am learning selenium, as part of i was trying to execute the below program. But i was getting the error " Cannot find class in classpath:practiseAutomation". The code is below:
public class practiseAutomation {
public WebDriver driver;
String baseurl="http://www.ticketnew.com/";
#BeforeTest
public void beforeTest() {
System.out.println("Executing Before Test Block");
System.setProperty("webdriver.ie.driver", "c://IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
driver.get(baseurl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
#Test
public void test()
{
System.out.println("Executing Test Block");
System.out.println("The Page titile is "+ driver.getTitle());
}
}
#AfterTest
public void afterTest()
{
System.out.println("Executing After Test Block");
driver.close();
}
}
Kindly help me in resolving the above issue.
In your BeforeTest method, you are creating webdriver instance which has scope in beforeTest method only. It will throw null pointer in #Test and #AfterTest methods.
As you have already declared driver globally, so just initialize it in BeforeTest method like below:-
driver=new InternetExplorerDriver();
and not WebDriver driver=new InternetExplorerDriver();
Related
Find below my code
TestBase.java
protected WebDriver driver;
#BeforeMethod
public void setUpDriver() {
System.setProperty("webdriver.chrome.driver", getConfigProp.getChromeDriverPath());
driver = new ChromeDriver();
}
#AfterMethod
public void quitDriver() {
driver.close();
driver.quit();
}
MyTests.java extends TestBase.java
#Test
Test1 (){
driver.doSomething()
}
#Test
Test2 (){
driver.doSomething()
}
Error:
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
I understand it is because of driver.quit(). So I used only driver. close() in AfterMethod. Still failed. I am not using static as well. What is the Solution.
I got it working using Threadlocal driver. Thanks
Can some one explain why i am getting Null pointer exception when code execution reaches login() method in below script
public class TC_01_CreateEmployee {
WebDriver driver=null;
public void launchBrowser() throws Exception
{
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
public void login()
{
driver.get("******");
driver.findElement(By.id("txtUsername")).sendKeys("****");
driver.findElement(By.id("txtPassword")).sendKeys("****");
driver.findElement(By.id("btnLogin")).click();
}
WebDriver driver=new ChromeDriver(); - This driver has a scope only within the method. The driver object which is used on the login method is still null. I am not sure why you would need 2 driver objects. you have 2 options to solve this,
public void launchBrowser() throws Exception
{
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
this.driver = driver;
Thread.sleep(2000);
}
Or
public void launchBrowser() throws Exception
{
driver=new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
It is likely that the launchBrowser() method is not being called before the login() method is being called.
An easy way around this is to define a getDriver() method that calls launchBrowser if driver is null.
private WebDriver getDriver() {
if (driver == null) {
launchBrowser();
}
return driver;
}
Then your login method looks like:
WebDriver driver = getDriver();
driver.get("******");
driver.findElement(By.id("txtUsername")).sendKeys("****");
driver.findElement(By.id("txtPassword")).sendKeys("****");
driver.findElement(By.id("btnLogin")).click();
I have written the below code and getting the java null pointer exception.
please help what is the problem.
public class testngbasics {
WebDriver driver;
#BeforeMethod
public void setbrowser() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
driver.get("http://demo.guru99.com/v4/");
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
}
#Test
public void login1() {
driver.findElement(By.xpath("//input[#name='uid']")).sendKeys("mngr212595");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("EgebYpy");
driver.findElement(By.xpath("//input[#value='LOGIN']")).click();
System.out.println(driver.getTitle());
}
#AfterMethod
public void closebrowser()
{
driver.close();
}
}
I am getting the error:
java.lang.NullPointerException it is only going to #beforemethod
annotation and not going to #test and #aftermethod
As you have already defined a global instance of WebDriver as driver in the line:
WebDriver driver;
You don't have to create another instance of WebDriver again as in:
WebDriver driver= new ChromeDriver();
And you need to replace this line as:
driver = new ChromeDriver();
I'm running my selenium code for Page object model using testNG, #BeforeTest will run only once for every test and #BeforeMethod will run everytime for every test. So which annotation would be preferable to use for page object model ?
#BeforeMethod
public void setUp() throws IOException
{
initialisation();
loginPage = new Login();
loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
ot = new OpenTasks();
active = new ActiveProjects();
}
#Test(priority=1)
public void validateTitle() throws IOException, InterruptedException
{
String custTitle = ot.verifyTitle();
Assert.assertEquals(custTitle, "actiTIME - Open Tasks", "Title not matched");
}
#Test(priority=2)
public void verifyProjectLink() throws IOException, InterruptedException
{
active = ot.clickOnProjectLink();
}
#AfterMethod
public void tearDown()
{
driver.quit();
}
(or)
public static WebDriver driver;
#BeforeTest
public void preConfig()
{
if(browser.equals("firefox"))
{
System.setProperty("webdriver.gecko.driver","C:\\Program Files\\FireFox\\geckodriver-v0.24.0-win64\\geckodriver.exe");
driver=new FirefoxDriver();
}
if(browser.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Chrome\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();
}
driver.get("http://desktop-g53h9ip:81/login.do");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#AfterTest
public void postConfig()
{
driver.close();
}
It is better to instantiate browser once for a single test and 2nd approach is better approach for any kind of framework.
Following structure for TestNG annotations
#BeforeSuite
#BeforeTest
#BeforeClass
#BeforeMethod
#Test
#AfterMethod
#AfterClass
#AfterTest
#AfterSuite
I have downloaded all the JUnit jar files and installed them.When I run my package as JUnit,it says no tests found with TestRunner 'JUnit 4'.What am I missing
*public class AmazonTestFactory{
static WebDriver driver;
#Before
public void setUp() throws Exception {
driver= new FirefoxDriver();
driver.get("http://www.amazon.co.uk");
Thread.sleep(5000);
driver.manage().window().maximize();
}
#Test
public void testHomePage(){
AmazonHomePageFactory Page = PageFactory.initElements(driver, PageFactoryModelExercise.AmazonHomePageFactory.class);
Page.enterProductInSearchBox();
Page.clickSearchButton();
}
#After
public void closeBrowser(){
driver.close();
}*