Not all test in the suite are executed in TestNG - selenium

I'm using eclipse to execute the suite but only the third test case (Test3) cannot be executed. After (Test2) executed, it will jump to the (Test4) and not (Test3).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SeleniumTestSuite" verbose="1">
<test name="Test1">
<classes>
<class name="sel1318_usercreation_author.UserCreation"></class>
</classes>
</test>
<test name="Test2">
<classes>
<class name="sel1319_userprofileupdate_author.UserProfileUpdate"></class>
</classes>
</test>
<test name="Test3">
<classes>
<class name="sel1320_customercreation_corporate.CustomerCreation"></class>
</classes>
</test>
<test name="Test4">
<classes>
<class name="sel1321_customerdeletion_corporate.CustomerDeletion"></class>
</classes>
</test>
<test name="Test5">
<classes>
<class name="sel1322_userinactive_author.UserInactive"></class>
</classes>
</test>
<test name="Test6">
<classes>
<class name="sel1323_userdeletion_author.UserDeletion"></class>
</classes>
</test>
</suite>
This is the code for Test4. Basically this test is to delete the customer thus, Test3 will create the customer. Besides, the user password will be changed in Test3. So when TestNG jump to the Test4, the user cannot login because the password should be different.
package sel1321_customerdeletion_corporate;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.Test;
#Test
public class CustomerDeletion {
{
System.setProperty("webdriver.gecko.driver","C:\\selenium\\geckodriver-v0.23.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("");
driver.manage().window().maximize();
driver.switchTo().frame("containerFrame");
//Login Author
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#name='userName']")));
driver.findElement(By.xpath("//input[#name='userName']")).click();
driver.findElement(By.xpath("//input[#name='userName']")).sendKeys("sele1");
driver.findElement(By.xpath("//input[#name='password']")).click();
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("password1");
driver.findElement(By.name("submitLogin")).click();
//Delete Customer
driver.findElement(By.id("menu5")).click();
Actions hover = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//div[#id='hel19']/div"));
hover.moveToElement(element).build().perform();
driver.findElement(By.id("el2")).click();
driver.findElement(By.name("customerName")).sendKeys("Selenium_Cust39");
driver.findElement(By.id("AddNew24")).click();
driver.findElement(By.linkText("SELENIUM_CUST39")).click();
driver.findElement(By.linkText("Delete")).click();
driver.findElement(By.id("AddNew24")).click();
Alert alt = driver.switchTo().alert();
alt.accept();
driver.findElement(By.linkText("Logout")).click();
driver.close();
Assert.assertEquals("Pass", "Pass");
}
}

In your class CustomerDeletion whole code is written in an anonymous block, because of which it is not getting executed when you are trying to run it through your testng.xml.
You should put the code in a method inside the class and then the code will be executed through the testng.xml
For Example:
#Test
public class CustomerDeletion {
//Make a method inside which you will be writing the whole code
public void customerDeletionMethod(){
//Copy Paste your code here
}
}

Related

How to launch & use separate browser for each test method inside a class

I have the below scenario
All the 3 tests run, but they are sharing only 1 browser. I have all the common methods for all the clicks etc in a Base class.
I want each of the test method -method1/2/3 to launch different browser & work,
Can somebody help?
Class A extends BaseTest{
#BeforeMethod(){
initDriver();// it does setdriver & getDriver is used across
}
public void doStuff(){
...
}
#Test
public void method1(){
doStuff()
}
#Test
public void method2(){
doStuff()
}
#Test
public void method3(){
doStuff()
}
}
You can use TestNG.xml to parameterized and control your execution as below. Here we ahve created 3 different tests , each one of them is parameterized with browser type variable and running one of your #test method.
TestNG.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="TestChrome">
<parameter name="browser" value="chrome"/>
<classes>
<class name ="<Full path to your Test class>" />
<methods>
<include name="method1" />
</methods>
</classes>
</test>
<test name="TestForefox">
<parameter name="browser" value="firefox"/>
<classes>
<class name ="<Full path to your Test class>" />
<methods>
<include name="method2" />
</methods>
</classes>
</test>
<test name="TestIE">
<parameter name="browser" value="edge"/>
<classes>
<class name ="<Full path to your Test class>" />
<methods>
<include name="method3" />
</methods>
</classes>
</test>
</suite>
Get the browser type parameter in your #BeforeMethod.
#Parameters ({"browser"})
#BeforeMethod(){
initDriver(browser);/* it does setdriver & getDriver is used across. Passing browser name
to initDriver method.*/
}
Now in initDriver() method ( wherever you have implemented it), set driver based on your browser type. Something similar to below:
public void initDriver(String browser) throws Exception{
if(browser.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.gecko.driver", ".\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if(browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver",".\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browser.equalsIgnoreCase("Edge")){
System.setProperty("webdriver.edge.driver",".\\MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}
else{
throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.window().maximize();
}
Note: Using include/exclude tag in TestNg.xml file you can run/ignore any #Test methods inside a test.

How to run test cases in different Java classes in a new window every time using TestNG and Selenium

I have 15 test methods in 3 Java classes (Selenium Script). I want to run each Test class with new window. I am using TestNg framework.
Here is the code of TestNG:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Fanfight Test" thread-count="10" parallel="methods">
<listeners>
<listener class-name="com.fanfight.test_case.ListenerClass">
</listener>
</listeners>
<test name="User Login" parallel="false">
<classes>
<class name="com.fanfight.test_case.UserLogin"></class>
</classes>
</test>
<test name="Contest Creation" parallel="false" >
<classes>
<class name="com.fanfight.test_case.ContestCreation"></class>
</classes>
</test>
<test name="User Profile Test" parallel="false" >
<classes>
<class name="com.fanfight.test_case.UserProfileTest"></class>
</classes>
</test>
<test name="Menu Bar Test" parallel="false">
<classes>
<class name="com.fanfight.test_case.MenuBarTest">
</class>
</classes>
</test>
<test name="Home Page Elements" parallel="false" >
<classes>
<class name="com.fanfight.test_case.HomePageElementTest"></class>
</classes>
</test>
</suite>
Without using parallel="false" my script is running in alphabetical order due to which selenium unable to find the path and execution got stuck.
Also please suggest how to make execution continue even after getting an exception during execution.
Add a setup and tear down method in each of the 3 test classes. The setup method should launch the browser and teardown method should close that browser instance.
class TestOne {
WebDriver driver;
#BeforeClass
public void setup(){
driver = new ChromeDriver();
}
#Test
public void testCase1(){
}
//.... Other test methods
#AfterClass
public void tearDown(){
driver.quit();
}
You can also create a parent class having just the setup and tear down methods , pseudo coded above. All 3 of your test class shall extend this parent class. It will be an optimised approach as the driver instantiation and destruction is now centralised to a single class.
And finally , change the parallel attribute in the suite tag of your testNG xml, in order to make them run parallel.
<suite name="Fanfight Test" thread-count="10" parallel="classes">

How to create testng.xml with following requirements?

Run tests in three browsers (chrome, firefox and ie) parallely. Each browser should open 2 instances. In total, on triggering testng.xml , 6 browser instances should be opened.
<suite thread-count=3 parallel="tests">
<test>
for firefox
</test>
<test>
for chrome
</test>
<test>
for ie
</test>
</suite>
Please help me!
In your TestNG.xml file , add a parameter for specifying browser type.
<test>
<parameter name="browser" value="firefox">
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.parameterization.TestParameters" />
</classes>
</test>
<test>
<parameter name="browser" value="chrome">
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.parameterization.TestParameters" />
</classes>
</test>
<test>
<parameter name="browser" value="ie">
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.parameterization.TestParameters" />
</classes>
</test>
</suite>
In your test class receive these parameters and create a webdriver according to the desired capabilities.
package com.parameterization;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParameters {
#Parameters({ "browser", "username", "password" })
#Test
public void testCaseOne(String browser,String username, String password) {
System.out.println("browser passed as :- " + browser);
createWebDriver(browser);
loginToApplicationOne(username,password);
}
#Parameters({ "browser", "username", "password" })
#Test
public void testCaseTwo(String browser, String username, String password) {
createWebDriver(browser);
loginToApplicationTwo(username,password);
}
}
As you have set thread-count to 3 and your requirement is to launch 2 browser instances in each test block. You have to refactor your test classes in the above style so that each method block create its isolated driver instance. Thus a total of 6 browsers would be launched.
There is no clean way to do this through xml just workarounds.
You can add the invocationCount to the #Test annotation for the test you want to repeat. Refer to link for more details.
Also you could create a duplicate of the xml file and run them as parallel suites using -suitethreadpoolsize as argument. Also pass in both the xml files as argument. Refer to link for doc.
Plus as suggested in the answers before, copying the tests multiple times in same xml.

TestNG configuration failure

I am trying to run a simple TestNG test from the command line, it errors with a configuration failure. The same TestNG test will run from Eclipse IDE correctly. From command line it does not work. This is a severe limitation of this TestNG framework making it not very useable in a Continuous integration theme. If you plan on working with TestNG ensure you can get it running from the command line before committing to using it as your testing framework. Having to run it from Eclipse IDE is a severe limitation.
TestSuite_Bollosk
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
command line syntax is:
java -cp "C:\correctclpath1\*;C:\correctclpath2\*" org.testng.TestNG "C:\anotherpath\TS_simpletest.xml"
the test looks like this:
package pkgTSBollosk;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TC_Bollosk
{
WebDriver driver;
#Parameters({"param1","param2"})
#Test
public void bollosktestmethod(String param1, String param2) throws InterruptedException
{
assert(true);
System.out.println("test output for bollosk test:");
}
#BeforeTest
public void beforeTest() throws IOException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS);
}
#AfterMethod
public void afterTest() {
driver.quit();
driver = null;
}
}
the TS_simpletest.xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Bollosk Test Suit" parallel="false">
<test name="Bolluks test name">
<parameter name="Param1" value="not - used"></parameter>
<parameter name="Param2" value="not - used"></parameter>
<classes>
<class name="pkgTSBollosk.TC_Bollosk">
<methods>
<include name = "bollosktestmethod"></include>
</methods>
</class>
</classes>
</test>
</suite>
Substituting Assert.assertTrue for assert and importing org.testNG.assert fixed the problem. A trap for the newbie..... I retract my previous statement about severe limitation.
public class test1 {
public WebDriver d;
#Parameters({"browsername","url"})
#BeforeMethod
public void browserselections(String broname,String URL) {
System.out.println(broname);
System.out.println(URL);
if (broname.equalsIgnoreCase("ff")) {
d = new FirefoxDriver();
} else if (broname.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "F:\\All jars\\chromedriver.exe");
d = new ChromeDriver();
}
d.manage().window().maximize();
d.get(URL);
}
#Test
public void tc1() {
d.findElement(By.id("lst-ib")).sendKeys("testing");
}
}
below is my xml code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" configfailurepolicy="skip">
<test name="Test1">
<parameter name="browsername" value="ff"/>
<parameter name="URL" value="https://www.google.com"/>
<classes>
<class name="testng.test1" />
</classes>
</test>
<test name="Test2">
<parameter name="browsername" value="chrome"></parameter>
<classes>
<class name="testng.test1" />
</classes>
</test>
</suite> <!-- Suite -->enter code here
I was facing the same issue and i noticed that all of my jar files were in different sub folders. Move all of them in one library folder and execute with same command.
java -cp "%projectLocation%/Library/*";"%projectLocation%/target/classes/" org.testng.TestNG testng.xml
Try TestNG config policy. Add parameter configfailurepolicy="continue" if you wish to proceed with config failures else, configfailurepolicy="skip". Example
<suite name="Bollosk Test Suit" configfailurepolicy="continue">
If you remove driver.quit() from public void afterTest() method,
the Configuration Failures will not come.

will testng initiate as many webdriver instance as test cases?

I am facing a problem of Selenium Grid and TestNG. My test scripts will NOT quite the browser until all the test class are executed.
I've configed 5 Chrome instance running on my Selenium Grid, then below is my xml file
<suite name="BrowserTest">
<test name="Test1" >
<parameter name="browserName" value="chrome"/>
<classes>
<class name= "Pagetest" />
</classes>
</test>
<test name="Test2" >
<parameter name="browserName" value="firefox"/>
<classes>
<class name= "Pagetest" />
</classes>
</test>
<test name="Overview Page Test on ie, English" >
<parameter name="browserName" value="ie"/>
<classes>
<class name= "Pagetest" />
</classes>
</test>
</suite>
Here is my test scripts
public class Pagetest {
private WebDriver browser;
public PateTest( String browser){
// create a browser instance.
}
#AfterClass (or #afterTest)
public void afterTest() throws Exception {
System.out.println("this test is done");
this.browser.quit();
}
#Test
public void test1(){
this.browser.get("www.google.com");
// doing some login stuff
}
}
Now when I am running my test, testNG will create 3 chrome webdirver instance one by one (with running the test). but after the 1st test is done, the browser instance of 1st test is not quited until all 3 tests are done, then all the 3 chrome instance are quited almost at the same time. What I want is after the 1st test is done, it's browser instance is quited, then the 2nd test case is started, did I do something wrong or how should I change my code?
Try using threadlocal it would help when you are even running tests in parallel
public ThreadLocal<WebDriver> driver=null;
#beformethod()
public void setUp
{
driver = new ThreadLocal<WebDriver>();
driver.set(new ChromeDriver());
return driver.get();
}
#AfterMethod
public void closeBrowser() {
{
driver.get().quit();
}