-1

im looking for solution to run test where are included test cases from several test cases. I need to make test like this: 1.open homepage 2.click on login link 3. type login and pass + submit

I have 2 classes (pages) HomePage and LoginPage, I want to make GotoLoginPageTest and LoginTest. I dont know how to make flow to run GotoLoginPageTest and in the same browser LoginTest (2 tests cases in one test). I dont know how to addict this two tests cases. Can You tell me how do do this, or some example using pagefactory, page object? Im using maven, testng, java. My CODE:

 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 -->

GotoLoginPageTest pass but LoginTest fail, I have error java.lang.NullPointerException.

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)

I know that in LoginPage is no Instanse of driver, but I do not want to create new driver (browser) becouse I want make this tests in one, the same browser.

I was trying use super(driver) but then even tests from GotoLoginPage was fails...

I just want to know what to do to make possible run few testscases in one test using project structure like I use now (pages classes + test classes)

Base Class:

       public class BaseClass {
    public static WebDriver driver;

    public BaseClass(WebDriver driver) {
        BaseClass.driver = driver;
    }

}

HomePage:

public HomePage(WebDriver driver){
            super(driver);
        }

LoginPage:

public HomePage(WebDriver driver){
            super(driver);
        }

------======================UPLOADED 22 oct 2015================-------


BaseClass

public class BaseClass {
    public static WebDriver driver; --------------- CHANGED
public String PAGE_URL;
public String PAGE_TITLE;

public BaseClass(WebDriver driver) {
    BaseClass.driver = driver;---------------------- CHANGED
    PageFactory.initElements(driver, this);
}
}

HomePage

public class HomePage extends BaseClass {

    public HomePage(WebDriver driver) {
        super(driver);
        this.PAGE_TITLE = "title";
        this.PAGE_URL = "https://totest.com/";
    }

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

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

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

LoginPage

public class LoginPage extends BaseClass{

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

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

    public LoginPage(WebDriver driver){
        super(driver);
        this.PAGE_TITLE = "Login to Base";
    }

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

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

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

}

GotoLoginPage

public class GotoLoginPageTest {
------------------------------------------ CHANGED(removerd WebDriver driver)

    public HomePage hp;

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

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

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

}

LoginTest

public class LoginTest {
    ------------------------------------------ CHANGED(removerd WebDriver driver)
    public LoginPage lp;

    public void setup(WebDriver driver){
    lp = PageFactory.initElements(driver, LoginPage.class);
    }

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

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

}

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 -->

New ERROR!!!:

TestNG] Running:
  /tmp/testng-eclipse-1551592337/testng-customsuite.xml

PASSED: isHomePage
PASSED: gotoLoginPage
FAILED: cheskIsLoginPage
java.lang.NullPointerException
    at tests.LoginTest.cheskIsLoginPage(LoginTest.java:19)
    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)

FAILED: logInBase
java.lang.NullPointerException
    at tests.LoginTest.logInBase(LoginTest.java:24)
    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)


===============================================
    Default test
    Tests run: 4, Failures: 2, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 4, Failures: 2, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@6d86b085: 9 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 3 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@39a054a5: 6 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@6a38e57f: 4 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1f89ab83: 17 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@43556938: 4 ms
ciupakabrans
  • 45
  • 1
  • 2
  • 7

1 Answers1

0

As you are using TESTNG , it is very simple to run multiple methods/Class by one click.

You do not need different class for Home page , Login etc. You can do like this :

1 - Create common TestNG class

2 - Add Function/Method for Home page

3 - Add Function/Method for Login

METHOD 1 :

    @Test(priority = 0)
    public void Home() 
    {
      
       //LOGIC FOR HOME
    
    }

    @Test(priority = 1)
    public void Login() 
    {
      
       //LOGIC FOR LOGIN
    
    }

If you want to use your current classes then just convert your existing classes to TESTNG class and add one new TESTNG class to call method of those classes.

METHOD 2 :

@Test(priority = 0)
public void Home() 
{
  
  //  Yourclassname.(dot)method name

}

@Test(priority = 1)
public void Login() 
{
  
   //  Yourclassname.(dot)method name

}

So this is how you can run multiple things in selenium using TESTNG by one click and one class only.

METHOD 3 :

In Addition you can run test via XML also , That steps are given below :

1 - Create new xml file by right click on your project.

2 - Paste below code in xml

  <suite name="Your Test Suite Name">
    <test name="Your Test Name>
        <classes>
            <class name="Yourpackage.(dot)classname"/> //This is that common class which we have created above
        </classes>
    </test>
</suite>

Above will allow you to run all tests by just one xml.

As per your updated question , Your driver getting null as declaration and calling classes are different.

So you should declare driver :public static WebDriver driver;

Then when you are calling to other class then it will be like : GotoLoginPageTest.driver.getTitle(); Also you should remove all driver declaration from all other classes once you apply above.

One more thing that for to declare driver globally , you can create base class and add method in a way like :

public static WebDriver driver;
        
        
        //CHECK FOR DRIVER AND CALL IT
        public static WebDriver getdriver()
        {
            
            if(driver==null)
            {
                
                setup();
                
                
            }
            
            return driver;
            
        }

To call it in other class , it will be Base.getdriver().yourproperties

Community
  • 1
  • 1
Helping Hands
  • 5,292
  • 9
  • 60
  • 127
  • Thanks for answer, but the problem is that there will be a lot of Pagesclsss. I want it will clean and good to read code. – ciupakabrans Oct 20 '15 at 06:28
  • Yes that is fine , but do you have method wise code in classes? – Helping Hands Oct 20 '15 at 06:32
  • look at my question, ontop, I added code + some info – ciupakabrans Oct 20 '15 at 06:39
  • I have 2 question to your answer: 1. In which class should I declare it? 2. Is declaring driver static is right? Becouse I want to learn the best solution/practice in webdriver to find work I really want. So Im looking for best solution/template/flow of tests in selenium, – ciupakabrans Oct 20 '15 at 07:04
  • This is best practice , Yes once you declare driver as static then only you can access in other class using dot and also it will reduce you efforts that you will have no need to declare driver in every class. – Helping Hands Oct 20 '15 at 07:16
  • But wasn't be better if I will use BaseClass with declaring driver and extends Pages classes by BaseClass and add super(driver)? Is it not cleaner solution? – ciupakabrans Oct 20 '15 at 07:26
  • I thought about something like I made, look my Question, (baseClass and after), it looks nice and in base class I canmake other useful methods. But There is problem that it dont work, I get: java.lang.NullPointerException. I was trying to do this but it didnt want to work right. Mayby U know what I made wrong? – ciupakabrans Oct 20 '15 at 07:39
  • can you make it static ? public WebDriver driver; change to public static WebDriver driver; – Helping Hands Oct 20 '15 at 08:18
  • Ok, i will try it at home. If i will have problems I will write! Thanks a lot!!:) – ciupakabrans Oct 20 '15 at 08:38
  • Its not working... First tests from GoToLoginPage passed, but test from LoginPage fails... in checkIsLoginPage(), i get error **"java.lang.AssertionError: expected [Login Page] but found [Home Page]"**, and in logInBase() i get **java.lang.NullPointerException**... I dont now what to do... I'm frustrated becouse try to do this 3rd day... – ciupakabrans Oct 20 '15 at 13:52
  • Okay , can you please look here? http://www.toolsqa.com/selenium-webdriver/page-object-model/ , It has same way you are using. – Helping Hands Oct 21 '15 at 03:11
  • This example is not the same I want to do, there all methods (from 2 Pages clsses) are in one test class. I want to make for every pageclass do a testcase and later this cases run in one test one after another is same browser – ciupakabrans Oct 21 '15 at 07:02
  • Maybe I should return homepage instance somewhere? Or not? – ciupakabrans Oct 21 '15 at 12:16
  • Did you remove WebDriver driver; from all class and put that only in base class? can you please share all class update code? – Helping Hands Oct 21 '15 at 12:19
  • I will update, in about 1,5 hour. If U want i can give you link to my project in github – ciupakabrans Oct 21 '15 at 12:27
  • I pasted my whole code under the: ------======================UPLOADED 21 oct 2015================-------. I'm waiting for some direction:) – ciupakabrans Oct 21 '15 at 16:54
  • Why still you have declared webdriver driver in another class also? declare driver in base call only and make it static , like : `public static WebDriver driver;` – Helping Hands Oct 22 '15 at 04:11
  • So in GotoLoginPageTEST and LoginTEST should i remove webdriver driver at the top? – ciupakabrans Oct 22 '15 at 05:27
  • ehhh... now in LoginTest.class in line "LoginPage lp = PageFactory.initElements(driver, LoginPage.class);" I have issue "driver cannot be resolved to a variable" and the same in GotoLoginPageTest.class in 1. driver = new FirefoxDriver(); 2. hp = PageFactory.initElements(driver, HomePage.class); 3. driver.get(hp.PAGE_URL); – ciupakabrans Oct 22 '15 at 06:48
  • okay , can you please look here , this is same issue like you are facing :http://stackoverflow.com/questions/21634417/method-public-static-org-openqa-selenium-webdriver-mytestng-baseurl-basic-erro – Helping Hands Oct 22 '15 at 06:54
  • I dont want to ufe static driver like in ur example. But Tell me what You think about this example http://www.slisenko.net/2014/06/22/best-practices-in-test-automation-using-selenium-webdriver/ ?? Should it works? – ciupakabrans Oct 23 '15 at 05:08
  • Helping Hands, can U answer me? – ciupakabrans Oct 26 '15 at 13:40