Todor Inchovski
Todor Inchovski

Reputation: 77

How to run a class which runs multiple Selenium classes?

I have a test which I would like to know if it's possible to run with one click. What I mean is I have a class which runs e other classes and another one which runs one (I am doing research right now, so the tests ran are quite basic. so I have the class

package testNGresearch;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import multipleTest.Loginer;

public class AllLoginTestsNG extends OneClickTest{

    public static WebDriver driver;
    String baseUrl = "https://9gag.com/";
    @BeforeTest
    public void openBrowser() {
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test(priority = 1)
    public void GetUrl() throws InterruptedException {
        driver.get(baseUrl);
        Thread.sleep(1500);
        Loginer.login(driver);
    }
    @Test(priority = 2)
    public void clickSignUp() {
        Loginer1.signUp(driver);
    }
    @Test(priority = 3)
    public void cancelSignUp() {
        Loginer2.cancelSignUp(driver);
        driver.close();
    }
    @Test(priority=4)
    public void navigationTests() throws InterruptedException {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(baseUrl);
        Thread.sleep(1500);
        NavigationTestNG.navigation(driver);
    }
}

and then another one (seperate file)

package testNGresearch;

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

public class Loginer extends AllLoginTestsNG{


    @Test
    public void login() {
        driver.findElement(By.xpath("/html/body/div[7]/div[1]/div[2]/div/div[3]/button[2]/span")).click();
        driver.findElement(By.xpath("//*[@id=\"jsid-login-button\"]")).click();
        driver.findElement(By.xpath("//*[@id=\"login-email-name\"]")).sendKeys("someuser");
        driver.findElement(By.xpath("//*[@id=\"login-email-password\"]")).sendKeys("somepass");
        driver.findElement(By.xpath("//*[@id=\"login-email\"]/div[3]/input")).click();
    }
}

Then the second one which is ran by the AllLogin class

package testNGresearch;

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

public class Loginer1 extends AllLoginTestsNG {

    @Test
    public static void signUp(WebDriver driver) {
        driver.findElement(By.xpath("//*[@id=\"jsid-signup-button\"]")).click();
    }

}

Now I have a class AllNavigation

package testNGresearch;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AllNavigationTestsNG extends AllLoginTestsNG {

    public static WebDriver driver;
    String baseUrl = "https://9gag.com/";
    @BeforeTest
    public void openBrowser() {
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    @Test(priority = 1)
    public void startNavigating (WebDriver driver) throws InterruptedException {
        driver.get(baseUrl);
        Thread.sleep(1500);
        NavigationTestNG.navigation(driver);
    }
}

Which runs

package testNGresearch;

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

public class NavigationTestNG extends AllNavigationTestsNG {

    @Test (priority = 1)
    public static void navigation(WebDriver driver) throws InterruptedException {
        Thread.sleep(500);
        driver.findElement(By.xpath("/html/body/div[7]/div[1]/div[2]/div/div[3]/button[2]/span")).click();
        Thread.sleep(500);
        driver.findElement(By.xpath("//*[@id=\"top-nav\"]/div/a")).click();
    }
}

So the question is - Is it possible to create a class which runs the AllLogin class and the AllNavigationTestNG class (which makes them run one after another) I created a OneClickTest class which throws errors

package testNGresearch;

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

public class OneClickTest {

    public static void main(String[] args) {

        WebDriver driver;
        AllLoginTestsNG.allLogin(driver);
        AllNavigationTestsNG.anotherLogin(driver);
    }
}

Upvotes: 0

Views: 1891

Answers (2)

eduPeeth
eduPeeth

Reputation: 1868

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite"  parallel="tests" verbose="1">
    <test name="testNGresearch" preserve-order="true">
        <classes>
            <class name="com.testNGresearch.AllNavigationTestsNG " />
            <class name="com.testNGresearch.AllLoginTestsNG" />
        </classes>
    </test> 
    <!-- Test -->
</suite> <!-- Suite -->
  1. Make sure you have correct package names when specifying classes under test.

  2. Make your test such that - lets say login - completely login fucntionality into one test.

  3. Put all your page objects into a separate class and you can make them static.
  4. Have only one class to take care of Webdriver related functions.

Note - Like login() inside Loginer class is not a good way as you are depending on some other function to take you to this screen. You should develop test as independent as they can be and put assertions pertaining to one inside itself. Let me know if any questions.

For your refernce,

package testNGresearch;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import multipleTest.Loginer;

public class AllLoginTestsNG{

    public static WebDriver driver;
    String baseUrl = "https://9gag.com/";
    @BeforeTest
    public void openBrowser() {
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test(priority = 1)
    public void GetUrl() throws InterruptedException {
        driver.get(baseUrl);
        Thread.sleep(1500);
     }

    @Test(priority = 2)
    public void cancelSignUp() {
        driver.close();
    }

}

Upvotes: 0

Ishita Shah
Ishita Shah

Reputation: 4035

You can achieve this by executing TestNG.XML suite file,

This is sample code reference :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite"  parallel="tests" verbose="1">
    <test name="My Account Test" preserve-order="true">
        <classes>
            <class name="myAccount.login" />
            <class name="myAccount.registration" />
            <class name="myAccount.forgotPassword" />
            <class name="myAccount.signOut" />
        </classes>
    </test> 
    <!-- Test -->
</suite> <!-- Suite -->

Here Login script will call first, after that Registration and so on in defined Order. By this way you can have effective communication to calling multiple class with Single Click of TestNG.XML suite file.

Upvotes: 1

Related Questions