2
votes

I have got some problems with my automation testing. I have nearly around 50 test cases in my Eclipse IDE. All test cases are in different classes. Also, I have got one BaseClass that contains @beforeclass and @afterclass. In @beforeclass, the browser opens, URL opens and website URL opens then it does login procedure. Then my test cases work. All of them begin with @Test annotation. I connect them with using TestNG suite. Base Class: My BaseClass.java class MyBaseClass.java class TestNg: My Suite Example of Test Case (Class): My Example Test Case (Class)

Here is my question: I want to use priority (like @Test (priority=1)) for these classes (test cases) for reducing the workforce. But when there is a problem with my codes; my automation test stops. I want to, it would continue besides the stop.

The second option is using TestNG. TestNG is ok but every case, the browser opens. How can I create a test like just opening one browser then run all test cases in that browser?

By the way here is my example test case for your imagine all picture:

-@beforeclass: open browser - open URL - login

@test1: go to products screen - click create a product - create a product with initial quantity - then click save button, it should be in the products screen again.

@test2: click create a product button again. - create a product without initial quantity - click save button, it should get an error. click the cancel button to continue the third @test

@test3: so on...

-@afterclass: close browser

I really appreciate for your help! Thank you!


Edit: I switched my codes as like this. Because I dont want to my automation opens a new browser over and over again. All my test cases relates with only one screen. (products) All I want is by ordering:

  1. Initiate @BeforeSuite (open browser, URL..)
  2. Initiate @BeforeClass(go to Products screen - because it is common screen for all cases)
  3. Initiate @Test for Test Case 1
  4. Initiate @AfterClass (go to Products screen again for a connection with test case 2
  5. Initiate @Test for Test Case 2

My Test NG class:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="AllTests" verbose="10" >

  <test name="prcr1.1" >
    <classes>
       <class name="com.example.product.prcr1dot1" />
    </classes>
  </test>

  <test name="prcr1.2" >
    <classes>
       <class name="com.example.product.prcr1dot2" />
    </classes>
  </test>
  </suite>

My Base Class:

public class BaseClass {
    protected WebDriver driver;

    @BeforeSuite

    public void openMyexample() throws InterruptedException {
        System.out.println("Initiate LoginTest Test...");
        driver = utilities.DriverFactory.open("chrome");
        driver.manage().window().maximize();
        driver.get("https://mystage.example.com/en/public/login/?isTestAutomationLive=true");
        System.out.println("example Page Has Been Opened..");

        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.elementToBeClickable((By.name("email"))));

        // Enter Username Section
        WebElement username = driver.findElement(By.name("email"));
        username.sendKeys("[email protected]");
        Thread.sleep(1000);
        wait.until(ExpectedConditions.elementToBeClickable((By.name("password"))));
        // Enter Password Section
        WebElement password = driver.findElement(By.name("password"));
        password.sendKeys("Test*01");
        Thread.sleep(1000);
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//form[@id='frmLogin']/button"))));

        // Click Login Button
        WebElement logInButton = driver.findElement(By.xpath("//form[@id='frmLogin']/button"));
        logInButton.click();

        // PAGE LOADER

        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//span[contains(.,'×')]"))));

        Thread.sleep(1000);

        // Integration Message Function WebElement - Option 1: WebElement
        WebElement popupCancelButton = driver.findElement(By.xpath("//span[contains(.,'×')]"));
        popupCancelButton.click();
        Thread.sleep(3000);
    }

    @BeforeClass
    public void openProducts() throws InterruptedException {

        WebDriverWait wait = new WebDriverWait(driver, 20);
        // From Dashboard Section to Product Section
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//a[contains(text(),'Products')]"))));
        WebElement productButton = driver.findElement(By.xpath("//a[contains(text(),'Products')]"));
        productButton.click();
        Thread.sleep(4000);

        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//button[contains(.,'Create Product')]"))));
        WebElement createProductButton = driver.findElement(By.xpath("//button[contains(.,'Create Product')]"));
        createProductButton.click();
        Thread.sleep(4000);
    }

    @AfterClass
    public void closeProducts() throws InterruptedException {

        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//a[contains(text(),'Products')]"))));
        WebElement productButton = driver.findElement(By.xpath("//a[contains(text(),'Products')]"));
        productButton.click();
        Thread.sleep(4000);

    }

    @AfterSuite
    public void closeMyexample() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, 60);

        Thread.sleep(4000);

        // Sign Out Thread.sleep(5000);
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//div[2]/i"))));
        WebElement logoutScrollDownButton = driver.findElement(By.xpath("//div[2]/i"));
        logoutScrollDownButton.click();
        Thread.sleep(3000);
        WebElement signoutButton = driver.findElement(By.xpath("//a[contains(.,'Sign Out')]"));
        signoutButton.click();

        // Close Browser
        System.out.println("Your Test Has Been Ended Successfully.");
        Thread.sleep(1000);
        System.out.println("Your Test is going to Close..");
        driver.quit();

    }

    // Sleep Function
    private void sleep(long m) {
        try {
            Thread.sleep(m);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

My Test Case 1:

package com.example.product;

import utilities.BaseClass;

//Test Case 1: PRCR - 1.1
//Creating a new product with a unique SKU 
public class prcr1dot1 extends BaseClass {
    @Test
    public void prcr1dot1() throws InterruptedException {

        WebDriverWait wait = new WebDriverWait(driver, 20);

        wait.until(ExpectedConditions.elementToBeClickable((By.name("sku"))));
        String uuid = UUID.randomUUID().toString();
        driver.findElement(By.name("sku")).sendKeys(uuid);

        driver.findElement(By.name("name")).sendKeys(uuid);

        WebElement packType = driver
                .findElement(By.xpath("//kendo-dropdownlist[@id='packTypeName']//span[@class='k-input']"));
        packType.click();
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))));
        driver.findElement(By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))
                .sendKeys(uuid);
        wait.until(ExpectedConditions.elementToBeClickable((By.id("btnCreateProductPackTypeName"))));
        WebElement createPackType = driver.findElement(By.id("btnCreateProductPackTypeName"));
        createPackType.click();

        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//button[contains(.,'SAVE')]"))));
        WebElement productSaveButton = driver.findElement(By.xpath("//button[contains(.,'SAVE')]"));
        productSaveButton.click();
        Thread.sleep(8000);
    }
}

My Test Case 2:

import utilities.BaseClass;

public class prcr1dot2 extends BaseClass {
    // Test Case 2: PRCR - 1.2
    // Creating a new product with the used SKU

    @Test
    public void prcr1dot2() throws InterruptedException {

        WebDriverWait wait = new WebDriverWait(driver, 20);

        wait.until(ExpectedConditions.elementToBeClickable((By.name("sku"))));
        driver.findElement(By.name("sku")).sendKeys("SKU08");

        String uuid = UUID.randomUUID().toString();
        driver.findElement(By.name("name")).sendKeys(uuid);

        WebElement packType = driver
                .findElement(By.xpath("//kendo-dropdownlist[@id='packTypeName']//span[@class='k-input']"));
        packType.click();
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))));
        driver.findElement(By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))
                .sendKeys(uuid);
        wait.until(ExpectedConditions.elementToBeClickable((By.id("btnCreateProductPackTypeName"))));
        WebElement createPackType = driver.findElement(By.id("btnCreateProductPackTypeName"));
        createPackType.click();

        JavascriptExecutor jsx = (JavascriptExecutor) driver;
        jsx.executeScript("window.scrollBy(0,450)", "");

        WebElement productSaveButton = driver.findElement(By.xpath("//button[contains(.,'SAVE')]"));
        productSaveButton.click();

        Thread.sleep(5000);
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//button[contains(.,'Create Product')]"))));

        WebElement createProductButton2 = driver.findElement(By.xpath("//button[contains(.,'Create Product')]"));
        createProductButton2.click();

        wait.until(ExpectedConditions.elementToBeClickable((By.name("sku"))));
        driver.findElement(By.name("sku")).sendKeys("SKU08");

        String uuid2 = UUID.randomUUID().toString();
        driver.findElement(By.name("name")).sendKeys(uuid2);

        WebElement packType2 = driver
                .findElement(By.xpath("//kendo-dropdownlist[@id='packTypeName']//span[@class='k-input']"));
        packType2.click();
        wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))));
        String uuid3 = UUID.randomUUID().toString();
        driver.findElement(By.xpath(
                "/html//platform-root/kendo-popup[@class='k-animation-container k-animation-container-shown']//input"))
                .sendKeys(uuid3);
        wait.until(ExpectedConditions.elementToBeClickable((By.id("btnCreateProductPackTypeName"))));
        WebElement createPackType2 = driver.findElement(By.id("btnCreateProductPackTypeName"));
        createPackType2.click();

        /*
         * WebElement productSaveButton2 =
         * driver.findElement(By.xpath("//button[contains(.,'SAVE')]"));
         * productSaveButton2.click(); Thread.sleep(5000); WebElement
         * productCancelButton2 =
         * driver.findElement(By.xpath("//button[contains(.,'CANCEL')]"));
         * productCancelButton2.click(); Thread.sleep(2000);
         * wait.until(ExpectedConditions.elementToBeClickable((By.xpath(
         * "//button[contains(.,'YES')]")))); WebElement productCancelYesButton =
         * driver.findElement(By.xpath("//button[contains(.,'YES')]"));
         * productCancelYesButton.click();
         */

    }

}
1
could you please show us what you have written? snippet of code if possible?Manoj Kengudelu
My suggestion: try to make all test cases totally separate. for example: if in your test case, you need to be logged in, try to check if you are logged in, then continue. if you are not logged in, so do log in. in this case, if any error happens you can simply close the browser and start it again without affecting your other test cases.0xM4x
Based on my experience, making test cases like a chain, will cause so many hardships and it's going to be hard to manage the test cases as the number of test cases increase.0xM4x
@ManojKengudelu I added all the informations! Thank you!Fenikssy
@M3trix Thank you for your advice but I couldnt do that. All test cases relates with each other and all I want to do is, do test case 1, go to products screen again and do test case 2Fenikssy

1 Answers

0
votes

I am not sure it will solve your problem or not. But This idea works for me. Using this way you can run all the test case on same browser without opening multiple times.

Here I have three class

test1.java

test2.java

test2.java

And Mainclass.java

Here is test1.java

package test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class test1 {

    @BeforeClass
    public void before(){

    }
    @Test
    public static void test(WebDriver driver){
        driver.get("https://www.google.com");
    }
    @AfterMethod
    public void after(){

    }
}

Here is test2.java

package test;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class test2 {
    @Test
    public static void test(WebDriver driver){
        driver.get("https://www.yahoo.com");
    }
}

Here is mainclass.java file where I passed driver session to the test

package test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Mainclass {

    public static void main(String[]args){
        WebDriver driver;
        driver=new ChromeDriver();
        test1.test(driver);
        test2.test(driver);
    }

}

Now you need to run mainclass from testing.xml file.

From the main class you can open browser one time and passed driver to your all the test case so it will use browser window to run the test case.

I am not sure any other ideal idea is available or not but it will surely work for you