1
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DifferentMeterLogin 
{
        public WebDriver driver;
        //public String baseUrl = "https://website.com/";
        //private String username="1003852@us";
        //private String password="password";


        @DataProvider
        public Object[][] testData() 
        {
            Object data[][]=new Object[2][2];

            data[0][0]="1003852@us";
            data[0][1]="password";

            data[1][0]="1003854@us";
            data[1][1]="password";
            return data;

        }
        @BeforeSuite
        public void setUp()
        {
             driver = new FirefoxDriver();
             driver.manage().window().maximize();
             driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }

        @AfterSuite
        public void testDown()
        {
            driver.quit();
        }


        @AfterTest
        public void logOff()
        {
            driver.navigate().to("https://website.com/Account/LogOff");
            //driver.findElement(By.xpath("//*[@id='logoffstatus']")).click()

        }

@Test(priority = 1,dataProvider = "testData")

        public void loginConsumerPortal(String username, String password)
        {
            System.out.println("Logging in...");
            driver.get("https://website.com/");
            driver.findElement(By.id("id_username")).clear();
            driver.findElement(By.id("id_username")).sendKeys(username);
            driver.findElement(By.id("id_password")).clear();
            driver.findElement(By.id("id_password")).sendKeys(password);
            driver.findElement(By.tagName("button")).submit();
            System.out.println("Submitted");
        }

        @Test(priority=2)
        public void isLoginSuccessfull() throws InterruptedException
        {
            System.out.println("Checking if successfull login");
            Assert.assertEquals("Dashboard", driver.findElement(By.xpath("//*[@id='ConsumerDashboard-cnt_int']/h4")).getText());
            Thread.sleep(5000);
        }



}

**--------------------------------------------------------------------------* *

I have different types of login credentials. I should be able to login with all types of id's and visit all menus. I am using only firefox and using TestNG data provider to provide data.

The problem is :

The test is using 1 browser, whereas it should have used multiple windows. Firefox handles sessions separately for all profiles i guess.

What I am thinking is there should be 2 windows of Firefox opened.

but that is not happening.

Thanks in Advance

1

A quick fix would be to unite both Test methods into one that has the full user session life-cycle from login to logout with all related tests. For example:

    // remove @AfterTest annotation
    public void logOff()
    {
        driver.navigate().to("https://website.com/Account/LogOff");
        //driver.findElement(By.xpath("//*[@id='logoffstatus']")).click()

    }

    @Test(priority = 1,dataProvider = "testData")
    public void loginConsumerPortal(String username, String password)
    {
        System.out.println("Logging in...");
        driver.get("https://website.com/");
        driver.findElement(By.id("id_username")).clear();
        driver.findElement(By.id("id_username")).sendKeys(username);
        driver.findElement(By.id("id_password")).clear();
        driver.findElement(By.id("id_password")).sendKeys(password);
        driver.findElement(By.tagName("button")).submit();
        System.out.println("Submitted");
        System.out.println("Checking if successfull login");
        Assert.assertEquals("Dashboard", driver.findElement(By.xpath("//*[@id='ConsumerDashboard-cnt_int']/h4")).getText());
        Thread.sleep(5000);

        // Run other checks

        logOff();
    }

If, however, your aim is to run tests while both users are logged in, you would need to use separate windows (not just separate tabs) of Firefox, as the same profile is shared by all Firefox tabs (this includes session cookies). It is possible, but the performance trade-off for starting up multiple Firefox processes is considerable (this would add to test execution time), so I would not recommend it unless it is really necessary. Also, the test structure would need adjustments, as for parallel tests you would need to separate actions, intended for each window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.