Run sequential test cases in the same browser Selenium Webdriver

0

Good day for everyone, I have a question about using selenium webdriver, and I would like to know how I can run test cases sequentially without closing the browser, for example:

Suppose I have the following test cases: 1. Login 2. Create Order 3. Consult order

Then there are three different test cases, where the third depends on the second, and the second depends on the first.

My problem is that when I run the test suite, the first case passes without problems, but for the second one I close the browser and open the other one, but when I try to create the request, obviously this is not logged because the session is already It has been lost.

So what could you do in this case to run the test cases sequentially one after the other without closing or opening the browser again?

Thank you very much in advance for all the collaboration.

    
asked by user3826049 06.02.2018 в 19:39
source

2 answers

0

In the end I solved my problem in the following way

package test_suite;

import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import pageObjects.CreateNewStudyPage;
import pageObjects.LoginPage;

@RunWith(Suite.class)
@SuiteClasses({
    LoginPage.class,
    CreateNewStudyPage.class,
    //OpenAllTabsAndExports.class
})

public class Test_Suite {   
    public static WebDriver driver;

    @BeforeClass
    public static void setUpClass() throws MalformedURLException {
        System.setProperty("webdriver.chrome.driver", "D:\WebDriver_Selenium/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.get("http://10.3.8.50");
    }

    @AfterClass
    public static void setDownClass() {
         driver.quit();
    }
}

First test performed by the application

package pageObjects;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import test_suite.Test_Suite;

public class LoginPage {


    @Test
    public void testLogin() {
        WebDriver driver = Test_Suite.driver;
        driver.findElement(By.id("sponsorname")).clear();
        driver.findElement(By.id("sponsorname")).sendKeys("Test");
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys("admin1");
        driver.findElement(By.id("pwd")).clear();
        driver.findElement(By.id("pwd")).sendKeys("123");
        driver.findElement(By.cssSelector("button.btn.btn-success")).click();
    }
}

Then without closing the browser, the test suite continues executing the following test

package pageObjects;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;

import test_suite.Test_Suite;

public class CreateNewStudyPage {
    private StringBuffer verificationErrors = new StringBuffer();

    @Test
    public void testCreateNewStudy() throws InterruptedException {
        WebDriver driver = Test_Suite.driver;

        if(isElementPresent(By.id("userMenu"), driver)) {
            driver.findElement(By.xpath("//*[@id='dropdownMenu1']/i/i")).click();

            for (int second = 0;; second++) {
                if (second >= 30) fail("timeout");
                try { if (isElementPresent(By.xpath("//*[@id='userMenu']/div/ul/li[3]/a"), driver)) break; } catch (Exception e) {}
                Thread.sleep(1000);
            }
            driver.findElement(By.xpath("//*[@id='userMenu']/div/ul/li[3]/a")).click();
        }
        for (int second = 0;; second++) {
            if (second >= 60) fail("timeout");
            try { if (isElementPresent(By.xpath("//input[@value='Add']"), driver)) break; } catch (Exception e) {}
            Thread.sleep(1000);
        }
        driver.findElement(By.xpath("//input[@value='Add']")).click();

        for (int second = 0;; second++) {
            if (second >= 60) fail("timeout");
            try { if (isElementPresent(By.id("StudyCode"), driver)) break; } catch (Exception e) {}
            Thread.sleep(1000);
        }
        driver.findElement(By.id("StudyCode")).clear();
        driver.findElement(By.id("StudyCode")).sendKeys("Study_Code_Automation28");
        driver.findElement(By.id("GSKBmarcref")).clear();
        driver.findElement(By.id("GSKBmarcref")).sendKeys("BMARCREF");
        driver.findElement(By.id("StudyShortName")).clear();
        driver.findElement(By.id("StudyShortName")).sendKeys("Short Name");
        new Select(driver.findElement(By.id("SvtVersionId"))).selectByVisibleText("1");
        new Select(driver.findElement(By.id("BudgetTypeId"))).selectByVisibleText("Substudy");
        new Select(driver.findElement(By.id("PhaseName"))).selectByVisibleText("I");
        new Select(driver.findElement(By.id("PatientTypeId"))).selectByVisibleText("Inpatient");

    private boolean isElementPresent(By by, WebDriver driver) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
}

Here the key is in the first lines of the classes where the test is going to take place, and it is to create the driver assigning the value of the driver of the Test Suite class

WebDriver driver = Test_Suite.driver;

And in this way the same browser is always used for all the tests that you run within the test suite.

    
answered by 08.02.2018 / 19:59
source
0

There are several ways to achieve what you propose. The simplest, but that goes against good testing practices is to have the three test cases on the same class, 3 @Test, and put the driver.quit in @afterclass. This way the browser will close after executing all the methods.

With the idea of the first step, you could also with TestNG create dependencies between the test cases, that is, case 2 depends on 1 and 3 of 2. In this way you get that if, for example, the login does not work, the other two tests do not run, their status would be 'Skiped'. TestNG also allows you to use the variable context of type ITestContext, where you can add properties. In this way you can share variables between different test cases.

Without changing anything about how the @beforeclass, @beforemethod and @afterclass or @aftermethod are defined, it is that even if you have 3 different test cases defined, when it comes to automating them, it is a single test with all the necessary steps so that the test validates the 3 test cases. You could add the corresponding assets to validate that tests 1 and 2 ended correctly.

    
answered by 07.02.2018 в 09:51