title: Selenium - Multi browser and multi user log in
body: <p>We are trying to start implementing automation for regression testing on our transaction based website. I successfully setup testng xml to open firefox, IE, and Chrome. And it will run the transaction class. In the setUP method with parameters it checks for browser and opens the correct browser for that thread. Then it is directed to the main() method test which starts up the Login class before coming back to the transaction class to actually do the transaction test. The reason for this is to have an organized structure that only has one test in each class for debugging purposes. </p>
  
  <p>When I run this test using a single user credential it runs fine and completes both tests. The problem is that our system allows a user to enter a transaction once every 10 seconds (just one of our security features to avoid refreshing or multiple transaction submissions). Any way, what is the best way to go about using multiple user credentials? I have tried multiple logic that I came up with but every time Firefox will use Username1 and both IE and Chrome will use either Username 2 or 3. </p>
  
  <p>In other words</p>
  
  <ul>
  <li>Firefox = Username1</li>
  <li>Chrome = Username2</li>
  <li>IE = Username2</li>
  </ul>
  
  <p>or </p>
  
  <ul>
  <li>Firefox = Username1</li>
  <li>Chrome = Username3</li>
  <li>IE = Username3</li>
  </ul>
  
  <p>I came up with a couple successful solutions but I had to duplicate classes or code.</p>
  
  <p>So one solution was to create separate Login classes for each browser as well as separate Transaction classes for each browser. And in the testng I ran the proper classes for each browser. I think this is a bad solution although it works.</p>
  
  <p>Another solution I came up with was taking the login test method and putting it into each if() statement in the parameters method of the Transaction class when it checks for each browser. Then each if statement would have its proper username. </p>
  
  <p>But I don't like this solution either because I would have to add this same code to all the future test classes that I will create if I am running a single test to check new code for a function being developed. Its just repeated code that I think should be in its own class. </p>
  
  <p>Here is the Login and Transaction classes before converting to testng. Also, I have made an Environment class that contains the static variables like username and url.</p>
  
  <p>P.S. I removed a bunch of test code not needed for this question.</p>
  
  <p>Ex:</p>
  
  <pre><code>static {
              URL = "https://website.com";
              DEFAULT_WAIT_TIME = 60;
              SYSTEM_ID = "IDnumber";
              USERNAME = "Username1";
              PASSWORD = "Password1";
      }
  </code></pre>
  
  <hr>
  
  <pre><code>import static org.junit.Assert.fail;
  
  import java.util.concurrent.TimeUnit;
  
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Test;
  import org.openqa.selenium.*;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.openqa.selenium.support.ui.ExpectedConditions;
  import org.openqa.selenium.support.ui.WebDriverWait;
  
  public class Login {
      private WebDriver driver;
      private String baseURL;
      private StringBuffer verificationErrors = new StringBuffer();
  
          @Before
          public void setUp() throws Exception {
              //driver = new FirefoxDriver();
              //baseURL = Environment.URL;
              driver.manage().timeouts().implicitlyWait(Environment.DEFAULT_WAIT_TIME, TimeUnit.SECONDS);
          }
  
          public static void doLogin(WebDriver driver, String baseURL) {
              WebDriverWait wait = new WebDriverWait(driver, Environment.DEFAULT_WAIT_TIME);
              System.out.println("Log in to trunk...");
              driver.get(baseURL);
  
              wait.until(ExpectedConditions.presenceOfElementLocated(By.id("company_id")));
              if (driver.findElement(By.xpath("//div[span='Log In']/span")).isEnabled()) {
                  System.out.println("    1 - 'Login' page is opened");
              }   else {
                      System.out.println("    !ERROR! - Can NOT open 'Login' page");
                      driver.findElement(By.id("This is to throw exception if Can NOT open 'Login' page"));
                  }
              driver.findElement(By.id("company_id")).clear();
              driver.findElement(By.id("company_id")).sendKeys(Environment.SYSTEM_ID);
              driver.findElement(By.xpath("//input[@name='username']")).clear();
              driver.findElement(By.xpath("//input[@name='username']")).sendKeys(Environment.USERNAME);
              driver.findElement(By.id("c_password")).clear();
              driver.findElement(By.id("c_password")).sendKeys(Environment.PASSWORD);
              System.out.println("    2 - Info filled");
  
              //I removed the rest of the code that finishes the login because          
              //I don't think it is needed for demonstration
  
          }
  
          @Test
          public void main() {
              doLogin(driver, baseURL);
          }
  
          @After
          public void tearDown() throws Exception {
                  driver.quit();
                      String verificationErrorString = verificationErrors.toString();
                          if (!"".equals(verificationErrorString)) {
                          fail(verificationErrorString);
                          }
          }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import static org.junit.Assert.fail;
  
  import java.util.concurrent.TimeUnit;
  
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Test;
  import org.openqa.selenium.By;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.openqa.selenium.ie.InternetExplorerDriver;
  import org.openqa.selenium.support.ui.ExpectedConditions;
  import org.openqa.selenium.support.ui.Select;
  import org.openqa.selenium.support.ui.WebDriverWait;
  
  public class Transaction {
      private WebDriver driver;
      private String baseURL;
      private StringBuffer verificationErrors = new StringBuffer();
  
      public static String country = "22";
      public static String ammount = "500";
  
          @Before
          public void setUp() throws Exception {
  //          System.setProperty("webdriver.ie.driver", "../IEDriverServer.exe");
  //          driver = new InternetExplorerDriver();
              driver = new FirefoxDriver();
              baseURL = Environment.URL;
  
              driver.manage().timeouts().implicitlyWait(Environment.DEFAULT_WAIT_TIME, TimeUnit.SECONDS);
          }
  
          public static void doTransaction(WebDriver driver) {
              WebDriverWait wait = new WebDriverWait(driver, Environment.DEFAULT_WAIT_TIME);
              System.out.println("Performing transaction...");
  
  
          // This is where the transaction code is ......
  
          }
  
          @Test
          public void transaction() {
          //Login first   
              Login.doLogin(driver, baseURL);
  
          // Transaction  
              doTransaction(driver);
          }
  
          @After
          public void tearDown() throws Exception {
                  driver.quit();
                      String verificationErrorString = verificationErrors.toString();
                          if (!"".equals(verificationErrorString)) {
                          fail(verificationErrorString);
                          }
          }
  }
  </code></pre>