I am trying to run the below code using TestNG,
package Framework;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
public class XMLtestng {
WebDriver d;
#Test
#Parameters({"fname"})
public void reg(String fname) throws InterruptedException {
d.findElement(By.linkText("REGISTER")).click();
d.findElement(By.name("firstName")).sendKeys(fname);
d.findElement(By.name("register")).click();
Thread.sleep(3000);
}
#BeforeClass
public void beforeClass() {
d = new FirefoxDriver();
d.manage().window().maximize();
d.get("http://newtours.demoaut.com/");
}
#AfterClass
public void afterClass() {
d.close();
}
}
And this is the corresponding xml file,
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="reg">
<Parameter name="fname" value="Rachel"/>
<classes>
<class name="Framework.XMLtestng"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
when I run my xml file as TestNg suite,only the website's url is being opened and close. Actions to be performed (typing the firstname for registration), is not being executed. This is the message being printed in the console,
Suite
Total tests run: 1, Failures: 0, Skips: 1
===============================================
Can anybody please help me in resolving this.
Thanks in advance.
Your test is skipped because you didn't provide it the expected parameter and it is not declared as #Optionnal.
The problem is you used <Parameter... instead of <parameter....
Use the DTD to validate your file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
Related
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
}
}
I am new to TestNg , Selenium.
My problem is I am not able to run my TestNG suite. When I click my testNg suite (By right clicking crossBrowser.xml and click TestNG Suite), It is not running, It does not show any error in the console.
Please find the code I am using.
VerifyTitle.java
package testNGTestPack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class VerifyTitle {
WebDriver driver;
#Test
#Parameters("browserName")
public void verifyTitle(String browserName){
if(browserName.equalsIgnoreCase("firebox")) {
driver=new FirefoxDriver(); }
else if(browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "D:\\driver\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver", "D:\\driver\\IEDriverServer.exe");
driver=new InternetExplorerDriver();
}
driver.manage().window().maximize();
driver.get("http://www.facebook.com");
//driver.quit();
}
}
crossBrowser.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="testNGTestPack">
<test name="Test">
<parameter name="browserName" value="firebox"/>
<classes>
<class name="testNGTestPack.VerifyTitle"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Eclipse Version : : Luna Service Release 2 (4.4.2)
When right clicking against crossBrowser.xml which is showing Run As TestNg Suite. But clicking on it, it does not trigger any action. Even in console, it show any error.
Do I need to include any jar file? Please give me a solution
Yes you need to create a new configuration for this.
Select your testng.xml file > Run as > Run Configuration > Click on TestNG on left panel > Right click and and CLick on new > Click on Suite > Browse xml and click on run.
Please try and let me know.
Thanks
Mukesh
I need to print this before actual execution of tests start. Anyways, we get the count at last of execution.
Say for example if my testng.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<groups>
<run>
<include name="functest" />
</run>
</groups>
<classes>
<class name="GroupTestExample" />
</classes>
</test>
</suite>
And there are 10 methods in class GroupTestExample which comes under group functest, it should print 10 in log as soon as it starts running.
Create a custom Listener which extends the org.testng.TestListenerAdapter. After that override the following method:
public class MyCustomListener extends TestListenerAdapter {
#Override
public void onStart(ITestContext testContext) {
super.onStart(testContext);
System.out.println("Number of Test Methods: " + testContext.getAllTestMethods().length);
}
}
You can add your custom listener by using #Listeners annotation on your main class of your test, like
#Listeners({MyCustomListener.class})
public class MyMainTestClass { ... }
You can find more information in TestNG doc:
TestNG Listeners -
http://testng.org/doc/documentation-main.html#testng-listeners
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.
Following is my test file :
package test;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
public class TestWatcherTest
{
#Rule
public TestWatcher testWatcher = new TestWatcher()
{
protected void failed(Throwable e, Description description)
{
System.out.println("in failed()");
}
};
#Test
public void shouldFail()
{
Assert.assertTrue("Test failed", false);
}
}
Output is :
<failure message="Test failed" type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedError: Test failed at test.TestWatcherTest.shouldFail(TestWatcherTest.java:24)
</failure>
It seems failed() is not being executed. I read many posts but nothing seems to work for me. What am I doing wrong?
The issue was with ant configuration. From http://testautomationusingjunit.blogspot.com/2013/08/applying-rules-to-junit-tests-running.html, the target needs to be edited to contain JUnitCore in the classpath.
<target name="junit" depends="build">
<java classname="org.junit.runner.JUnitCore">
<classpath>
<path location="selenium_server/selenium-server-standalone-xxx.xx.jar"/>
</classpath>
</java>
<junit fork="no" haltonfailure="no" printsummary="true" failureProperty="test.failed" dir=".">
<test name="src.SampleTest" todir="./report" />
</junit>
</target>