0

i have problem with my tests in WebDriver.

In first package I Have 2 classes (pages) HomePage, LoginPage.

In secound package i have test - goToLiginPageTest and LoginTest.

In goToLiginPage I check, I'm on homepage and go to login page.

In LoginTest I check that I'm on login page and login.

But two tests from goToLiginPageTest pass, but test from LoginTest fail.

I'm not sure that is somenthing wrong with my textng.xlm, or where I make mistake. Please help me.

    public class HomePage {
     WebDriver driver;  

     public static final  String PAGE_TITLE = "page title";
     public static final  String PAGE_URL = "www.blbl.pl";

@FindBy(xpath = "//*[@id='global-nav']/div/div/div[3]/ul/li[1]/a")
WebElement LogInLink;

    public HomePage(WebDriver driver){
        this.driver = driver;
    }

    public void isHomePage(){
        String pageTitle = driver.getTitle();
        Assert.assertEquals(pageTitle, PAGE_TITLE);
    }

    public void goToLoginPage(){
        LogInLink.click();
    }
}

LoginPage

public class LoginPage {
WebDriver driver;

public static final  String PAGE_TITLE = "Login";

@FindBy(id="user_email")
WebElement inputUserEmail;

@FindBy(id="user_password")
WebElement inputUserPassword;


public LoginPage(WebDriver driver){
    this.driver = driver;
}

public void isLoginPage(){
    String pageTitle = driver.getTitle();
    Assert.assertEquals(pageTitle, PAGE_TITLE);
}

public void fillUserEmail(){
    inputUserEmail.sendKeys("asdfasd@gmail.com");
    Assert.assertEquals(inputUserEmail.getAttribute("value"), "asdfasd@gmail.com");
}

public void fillUserPassword(){
    inputUserPassword.sendKeys("123456");
    Assert.assertEquals(inputUserPassword.getAttribute("value"), "123456");
}

}

GotoLoginPageTest

import pages.HomePage;

public class GotoLoginPageTest {
    WebDriver driver;
    HomePage hp;


    @BeforeClass
    public void setup(){
        this.driver = new FirefoxDriver();
        hp = PageFactory.initElements(driver, HomePage.class);
        driver.get(HomePage.PAGE_URL);
    }

    @Test(priority = 1)
    public void isHomePage(){
        hp.isHomePage();
    }

    @Test(dependsOnMethods = "isHomePage")
    public void gotoLoginPage(){
        hp.goToLoginPage();
    }

}

LoginTest

public class LoginTest {
    WebDriver driver;

    LoginPage lp = PageFactory.initElements(driver, LoginPage.class);

    @Test
    public void cheskIsLoginPage(){
        lp.isLoginPage();
    }

    @Test
    public void logInBase(){
        lp.fillUserEmail();
        lp.fillUserPassword();
    }

}

my testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test">
    <classes>
      <class name="tests.GotoLoginPageTest"/>
      <class name="tests.LoginTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

i have error

java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy5.sendKeys(Unknown Source)
    at pages.LoginPage.fillUserEmail(LoginPage.java:30)
    at tests.LoginTest.logInBase(LoginTest.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:659)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:771)
    at org.testng.TestRunner.run(TestRunner.java:621)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
    at org.testng.SuiteRunner.run(SuiteRunner.java:259)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
    at org.testng.TestNG.run(TestNG.java:1032)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
ciupakabrans
  • 45
  • 1
  • 2
  • 7

1 Answers1

3

You have never instantiated the Driver in LoginTest class. Instantiate the driver there and pass that along in the page object. Something like this:

public class LoginTest {
    WebDriver driver;
    driver = new FirefoxDriver();

    LoginPage lp = PageFactory.initElements(driver, LoginPage.class);

    @Test
    public void cheskIsLoginPage(){
        lp.isLoginPage();
    }

    @Test
    public void logInBase(){
        lp.fillUserEmail();
        lp.fillUserPassword();
    }
}
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • But I dont want to create new FFdriver, i want that after go to login page, login into. In the same test. I just wanto run GotoLoginPageTest and LoginTest one after the other. Im not sure but I think test run in bad order, firstly LoginTest and after GotoLoginPageTest. I put here screen from eclipse http://zapodaj.net/c776122be852e.png.html – ciupakabrans Oct 18 '15 at 17:40
  • You have to otherwise you are passing a null driver to the Loginpage. On the other hand, you should be creating the driver globally then instead of creating in each test object. See and example [here](https://github.com/safrrhmn/SeleniumTestNG/blob/master/src/main/java/com/github/tests/GitHubHomePageTests.java) – Saifur Oct 18 '15 at 17:45
  • Sorry for my questions, but I dont want to create new firefoxDriver object in each tests. I just want to make test where will run two cases in the one test. If I create new ffdriver, then will be open new browser, I want do this two case in one browser, one after another – ciupakabrans Oct 18 '15 at 20:18
  • either put the tests in the same class or you need to share the driver between 2 classes, otherwise I don't see how the instance from the first class gets injected into the second class without any additional code... – rac2030 Oct 19 '15 at 22:43