Akash
Akash

Reputation: 115

How to run two different page object methods in a single test case in webdriver using POM

I have created a POM model in my webdriver framework where i have two pages one is login page and the other is users page, now i have written a test case for login page and it is working fine, the problem comes when i am trying to run a second test case where i need to login first in order to reach to the users page where i need to click on the view page.

below is the piece of code which i am writing to run the two different page object model methods into a single test case , currently it is running the login method only not the users page method.![enter image description here][1] Above i have attached the framework screenshot, and below is the userstestcase code which i have written :

It is not allowing me to paste the framework screenshot please help me out

package testCases;    
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import pageFactory.Userspage;
import pageFactory.loginPage;


    public class UsersTestCase {

        WebDriver driver;
        loginPage lpg;
        Userspage upg;


        @BeforeTest
        public void setup(){

            driver = new FirefoxDriver();    
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("someurl");

        }

        @Test(priority=0)

        public void test_login(){

            lpg = new loginPage(driver);
            String loginPageTitle = lpg.getLoginTitle(); 
            Assert.assertTrue(loginPageTitle.contains("Login")); 
            lpg.loginToGuru99("username", "password");
            driver.findElement(By.xpath("//a[@href='somelink']")).click();
        }      

        public void test_users(){
            upg = new Userspage(driver);  
            String usersPageTitle = upg.getLoginTitle(); 
            Assert.assertTrue(usersPageTitle.contains("Users"));   



    }
    }

Upvotes: 2

Views: 5046

Answers (2)

Akash
Akash

Reputation: 115

The above question has been resolved what i have done to resolve this is i haven't declared the global webdriver command and then using this command i wasn't transferring the control to the users page but i have recreated my framework and everything is working fine now

Upvotes: 0

Pippo
Pippo

Reputation: 925

EDIT: a quick fix would be:

Move the below

lpg = new loginPage(driver);
upg = new Userspage(driver); 

to the beforeTest step, right under:

driver.get("http://citysurfstaging.sourcefuse.com/admin/login");

and in your user test, before anything else, call the line below:

lpg.loginToGuru99("[email protected]", "sourcefuse123");
driver.findElement(By.xpath("//a[@href='http://citysurfstaging.sourcefuse.com/admin/users']")).click();

That should put you in the state you need to perform the user test...

Quick advice, if you really want to do POM, then you shouldnt be mapping elements in your tests, that should all be done within your page class...

ORIGINAL:

If you have multiple pages, and need them to interact with each other to perform an end to end test, why dont you create another layer of abstraction to the POM and have a Flow class?

Example: In the constructor of the flow class you instantiate the pages you need to perform a flow (loginpage, userpage), which will give you visibility of the page objects of each page class, then create as many flows (methods) between these pages. The next step would be instantiate the flow in your test (the same way you instantiated the page) and call your flow methods which interacts with as many pages you wish... if this is not clear enough I can give more detailed examples

Upvotes: 2

Related Questions