Reputation: 19
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:
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();
*/
}
}
Upvotes: 2
Views: 3640
Reputation: 2707
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
Upvotes: 0