What to do if tests can run separately, but the same tests do not run together

with No Comments

Cookies handling with Selenium WebDriver (Java)

 

One of the reasons why my tests run separately and have a successful result, but failed when run all in one was cookies.

 Cookies are small files which are stored on your computer which web sites use to store client specific information e.g. Session identifiers, or tracking information.

 

 Every time when I tested a login form I had to clean a browser session to allow a user to open this login form with a new session. So, you can do this by simple deleteAllCookies() and after verify if cookies are deleted or not.

 

In this example cookies were deleted and their presence or absence was verified in every test case. If you comment block with Cookie handling this test will fail.

 

 

package my.main.dataqlick;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.Assert;

import org.testng.annotations.AfterClass;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;

 

public class MyTest {

 

  private WebDriver driver;

 

  @BeforeClass // Runs this method before the first test method in the current class is invoked

  public void setUp() {

      // Create a new instance of the Firefox driver

      driver = new FirefoxDriver();

  }

 

  @Test // Open browser and go to the Twitter to find element

  public void gotoAppleTwitterPage() {

      // Go to the Twiiter log in page

      driver.get(“https://twitter.com/login/”);

      // Find “Login” field

      driver.findElement(By.cssSelector(“.js-username-field”)).sendKeys(“test.by.me103@gmail.com”);

      // Find “Password” field

      driver.findElement(By.cssSelector(“div > div.signin-wrapper > form > fieldset > div:nth-child(3) > input”)).sendKeys(“Qwerty123!”);

      // Click on “Log in” button

      driver.findElement(By.cssSelector(“#page-container > div > div.signin-wrapper > form > div.clearfix > button”)).click();

      // Get text from header of the page

      String header = driver.findElement(By.xpath(“//*[@id=\”global-nav-home\”]/a/span[3]”)).getText();

      // Verify that header equals “Home”

      Assert.assertEquals(header, “Home”);

 

      // Delete all cookies on page

      driver.manage().deleteAllCookies();

      // Check all cookies is deleted

      String Cookie = driver.manage().getCookies().toString();

      System.out.println(“————————————->” + Cookie);

 

      // Go to the Twiiter log in page

      driver.get(“https://twitter.com/login/”);

      // Find “Login” field

      driver.findElement(By.cssSelector(“.js-username-field”)).sendKeys(“test.by.me103@gmail.com”);

      // Find “Password” field

      driver.findElement(By.cssSelector(“div > div.signin-wrapper > form > fieldset > div:nth-child(3) > input”)).sendKeys(“Qwerty123!”);

      // Click on “Log in” button

      driver.findElement(By.cssSelector(“#page-container > div > div.signin-wrapper > form > div.clearfix > button”)).click();

      // Get text from header of the page

      String header_1 = driver.findElement(By.xpath(“//*[@id=\”global-nav-home\”]/a/span[3]”)).getText();

      // Verify that header equals “Home”

      Assert.assertEquals(header_1, “Home”);

      // Delete all cookies on page

      driver.manage().deleteAllCookies();

      // Check all cookies is deleted

      String Cookie_1 = driver.manage().getCookies().toString();

      System.out.println(“————————————->” + Cookie_1);

  }

 

@AfterClass // Runs this method after all the test methods in the current class have been run

public void tearDown() {

  // Close all browser windows and safely end the session

  driver.quit();

  }

}

 

Leave a Reply