Second class of testing.xml doesn't execute - java

I have created 2 separate classes to test a webpage. But, unfortunately when I add them both to the testing.xml, only one of them execute and the other doesn't. The browsers open in parallel even after setting them to preserve-order="true" parallel="false" in the XML. I'm confused as to where I'm doing it wrong.
This is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" preserve-order="true" parallel="false">
<test name="Test">
<classes>
<class name="TestServiceNow.loginOne"/>
<class name="TestServiceNow.loginTwo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
loginOne is as follows:
package TestServiceNow;
import org.testng.annotations.Test;
import ServiceNow.login;
public class loginOne extends loginTest{
#Test
public void test_Login(){
//Create Login Page object
objLogin = new login(driver);
//login to application
objLogin.loginGurukula("admin", "admin");
}
}
loginTwo is as follows:
import org.testng.annotations.Test;
import ServiceNow.login;
public class loginTwo extends loginTest{
#Test
public void test_Login_Fail(){
//Create Login Page object
objLogin = new login(driver);
//login to application
objLogin.loginGurukula("admin", "admin1");
}
}
The base class is as follows:
public class loginTest {
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
File file = new File("C:/Users/gattu_000/Documents/selenium-java-3.0.0-beta2/chromedriver_win32/chromedriver.exe");
WebDriver driver;
login objLogin;
#BeforeClass
public void a() {
driver = new ChromeDriver(capabilities);
capabilities.setCapability("marionette", true);
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
System.out.println("Before class called");
}
#BeforeTest
public void setup(){
System.out.println("Before test called");
driver.get("http://localhost:8080/#/login");
}
#AfterTest
public void close() {
System.out.println("After test called");
}
#AfterClass
public void b() {
System.out.println("After class called");
driver.close();
}
}
The results look like
After the Edit

You are extending loginTest by both loginOne and loginTwo. But in loginTest you initialized your driver. That's why two browser are opening. To get around this issue, you can initialize your driver inside a setup method like #BeforeTest or #BeforeSuite. As an example here's a code snippet -
#BeforeSuite
public void a() {
driver = new ChromeDriver(capabilities);
System.out.println("Before suite called");
}
Do other things as usual like before except the initialization part.
Edit
I missed something. You are closing your driver at the after test method. To run your tests properly remove the driver.close() from your after test method and place it to aftereSuite section.

The XML is supposed to be like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" preserve-order="true">
<test name="Test">
<classes>
<class name="TestServiceNow.loginOne"/>
</classes>
</test> <!-- Test -->
<test name="Test1">
<classes>
<class name="TestServiceNow.loginTwo"/>
</classes>
</test>
</suite> <!-- Suite -->
To launch the browser twice, we need to have 2 separate tests. (Possibly, this may be one of the solutions out of many)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Selenium Test Suite">
<test name="Selenium Test Suite">
<classes>
<class name="packagename.classname1"/>
<class name="packagename.classname1"/>
</classes>
</test>
</suite>
Which is proper. If you are getting null point don't use driver in all the class. because of that only you are getting null pointer i guess.

Related

Parallel execution using TestNg Selenium in a POM framework overwriting the data object class

I am trying to run two test classes in parallel both are using data providers pointing to different excel sheets. Sequentially it runs fine, but while running it in parallel it seems like the test object class which is holding all the test data is getting overwritten. Below is the data provider
Class 1
#DataProvider(name = "tradeData" )
public Object[][] createMessage() {
return ExcelUtils
.getDataFromExcelSheet("TestSheet1", LOADTESTDATAPROVIDER);
}}
Class 2
#DataProvider(name = "tradeData")
public Object[][] createMessage() {
return ExcelUtils
.getDataFromExcelSheet("TestSheet2", LOADTESTDATAPROVIDER);
}
The data is being returned correctly from the excel but it seems like it is getting overwritten when placed into the object class. The data object class is being initialized from both test classes something like this
#Test(dataProvider = "tradeData")
public void connectAndPushMessage(String sellerName,
String buyerName,
String SIN){
testMessage tMessage = new testMessage();
tMessage.setSellerName(sellerName);
tMessage.setBuyerName(buyerName);
tMessage.setSIN(SIN);
}
Below is the testng.xml that I have used.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parallel Testing" parallel="classes" thread-count="2">
<test name="Test run 1">
<classes>
<class name="test.xxx.performance.InjectTrades_TestRun1"/>
<class name="test.xxx.performance.InjectTrades_TestRun2"/>
</classes>
</test>
</suite>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parallel Testing" parallel="tests" thread-count="2">
<test name="Test run 1">
<classes>
<class name="test.xxx.performance.InjectTrades_TestRun1"/>
</classes>
</test>
<test name="test run 2">
<classes>
<class name="test.xxx.performance.InjectTrades_TestRun2"/>
</classes>
</test>
I am getting the same result from both xmls so rather six distinct messages being created/pushed into the system(based on two excel sheets) the code is pushing three distinct messages repeated twice. I was/am under the impression that testNg should have take care of this as both classes/tests are running as separate threads.
EDIT
Below is the snippet of the Test object class. All attributes are declared private
public class testMessage{
private String currentBusinessDate;
private String sellerName = "";
private String buyerName= "";
private String sin= "";
....
public testMessage() {
cTradeID = "T" + CommonUtils.randomNumber(12);
sRef = CommonUtils.randomNumber(12);
requestId = "1" + CommonUtils.randomNumber(5);
settlementRequestStateCode = "New";
currencyTypeCode = "USD";
}
public String getCurrentBusinessDate() {
return currentBusinessDate;
}
public void setCurrentBusinessDate(String currentBusinessDate) {
this.currentBusinessDate = currentBusinessDate;
}
public String getsellerName () {
return currentBusinessDate;
}
public void setsellerName (String currentBusinessDate) {
this.currentBusinessDate = currentBusinessDate;
}
public String getBuyerName () {
return currentBusinessDate;
}
public void setBuyerName (String currentBusinessDate) {
this.currentBusinessDate = currentBusinessDate;
}
....
Used TestNG version is not mentioned, also ExcelUtils.getDataFromExcelSheet implementation is not shared, but both these 2 things might be related to such unexpected behavior.
I'm going to share my experiment result in order to confirm, that there is no issue with the TestNG parallelization itself, at least for TestNG 7.5.
Test Class 1
class T1 {
#Test(dataProvider = "tradeData")
public void connectAndPushMessage(String arg) {
System.out.println(arg);
}
#DataProvider(name = "tradeData")
public Object[][] createMessage() {
return new Object[]{
{ "T1.createMessage-data-1" },
{ "T1.createMessage-data-2" },
{ "T1.createMessage-data-3" },
};
}
}
Test Class 2
class T2 {
#Test(dataProvider = "tradeData")
public void connectAndPushMessage(String arg) {
System.out.println(arg);
}
#DataProvider(name = "tradeData")
public Object[][] createMessage() {
return new Object[]{
{ "T2.createMessage-data-1" },
{ "T2.createMessage-data-2" },
{ "T2.createMessage-data-3" },
};
}
}
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parallel Testing" parallel="tests" thread-count="2">
<test name="Test run 1">
<classes>
<class name="samples.T1"/>
</classes>
</test>
<test name="test run 2">
<classes>
<class name="samples.T2"/>
</classes>
</test>
</suite>
Output:
T1.createMessage-data-1
T2.createMessage-data-1
T2.createMessage-data-2
T1.createMessage-data-2
T2.createMessage-data-3
T1.createMessage-data-3
The same for
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parallel Testing" parallel="classes" thread-count="2">
<test name="Test run 1">
<classes>
<class name="samples.T1"/>
<class name="samples.T2"/>
</classes>
</test>
</suite>
Try to recheck this with TestNG 7.5, and look at your ExcelUtils.getDataFromExcelSheet method implementation, it should be thread-safe.

#AfterTest TestNG annotation is not called

I'm doing some tests using Java + TestNG, but I noticed that the tests are not executing the #AfterTest method. The browser remains open when the other tests are running (when it runs the first test, CreateNewUserWithValidData(), this one doesn't call the #AfterTest method, causing that the other tests fail). I need that every test call the #AfterTest method.
My testng.xml file has the following structure:
<suite name="Sample Tests" verbose="1" >
<listeners>
<listener class-name="Utilities.Listeners.TestListener"></listener>
<listener class-name="Utilities.Listeners.AnnotationTransformer"></listener>
</listeners>
<test name="Regression" >
<classes>
<class name="Tests.AutomationPracticesTests">
<methods>
<include name="CreateNewUserWithValidData" />
<include name="LoginWithAValidUser" />
<include name="LoginWithAnInvalidUser" />
</methods>
</class>
</classes>
</test>
</suite>
My BaseTest class looks like this.-
public class BaseTest {
protected String baseURL;
protected WebDriver driver;
protected WebDriverWait wait;
protected APAuthenticationPage apAuthenticationPage;
protected APCreateAccountPage apCreateAccountPage;
protected APHomePage apHomePage;
protected APMyAccountPage apMyAccountPage;
protected APShoppingCartAddressesPage apShoppingCartAddressesPage;
protected APShoppingCartOrderConfirmationPage apShoppingCartOrderConfirmationPage;
protected APShoppingCartOrderSummaryBankwirePage apShoppingCartOrderSummaryBankwirePage;
protected APShoppingCartPaymentMethodPage apShoppingCartPaymentMethodPage;
protected APShoppingCartShippingPage apShoppingCartShippingPage;
protected APShoppingCartSummaryPage apShoppingCartSummaryPage;
public WebDriver getDriver() {
return driver;
}
#BeforeTest(alwaysRun = true)
public void setUp() {
Log.info("I am in Before Method! Test is starting!");
driver = WebDriverFactory.getDriver(BrowserType.Chrome);
wait = new WebDriverWait(driver, 10);
driver.manage().window().maximize();
}
#BeforeMethod
public void initSetup() {
String propertiesFile = "data.properties";
PropertyReader propertyReader = new PropertyReader();
apAuthenticationPage = new APAuthenticationPage(driver);
apCreateAccountPage = new APCreateAccountPage(driver);
apHomePage = new APHomePage(driver);
apMyAccountPage = new APMyAccountPage(driver);
apShoppingCartAddressesPage = new APShoppingCartAddressesPage(driver);
apShoppingCartOrderConfirmationPage = new APShoppingCartOrderConfirmationPage(driver);
apShoppingCartOrderSummaryBankwirePage = new APShoppingCartOrderSummaryBankwirePage(driver);
apShoppingCartPaymentMethodPage = new APShoppingCartPaymentMethodPage(driver);
apShoppingCartShippingPage = new APShoppingCartShippingPage(driver);
apShoppingCartSummaryPage = new APShoppingCartSummaryPage(driver);
baseURL = propertyReader.getProperty(propertiesFile, "AUTOMATION_PRACTICE_URL");
}
#AfterTest(alwaysRun = true)
public void tearDown() {
Log.info("I am in After Method! Test is ending!");
driver.close();
driver.quit();
}
}
And my tests are the following ones.-
public class AutomationPracticesTests extends BaseTest {
// Properties
private String emailAddress, password;
// Tests
#Test(description = "It creates a new user in the store",
priority = 1)
public void CreateNewUserWithValidData(Method method) {
startTest(method.getName(), "It creates a new user in the store");
emailAddress = Mocks.personalData().get(0).getEmail();
password = Mocks.personalData().get(0).getPassword();
apHomePage.goTo(baseURL);
apHomePage.clickOnSignInButton();
apAuthenticationPage.fillCreateAccountForm(emailAddress);
apAuthenticationPage.clickOnCreateAccountButton();
apCreateAccountPage.fillRegisterForm(Mocks.personalData());
apCreateAccountPage.clickOnRegisterButton();
Assert.assertTrue(apMyAccountPage.isLoaded());
}
#Test(description = "It logins successfully in the store with a valid user",
priority = 2)
public void LoginWithAValidUser(Method method) {
apHomePage.goTo(baseURL);
apHomePage.clickOnSignInButton();
apAuthenticationPage.fillSignInForm(emailAddress, password);
apAuthenticationPage.clickOnSignInButton();
Assert.assertTrue(apMyAccountPage.isLoaded());
}
#Test(description = "It throws an error when the user attempts to login with an invalid user",
priority = 3)
public void LoginWithAnInvalidUser(Method method) {
apHomePage.goTo(baseURL);
apHomePage.clickOnSignInButton();
apAuthenticationPage.fillSignInForm(Mocks.invalidPersonalData().getEmail(), Mocks.invalidPersonalData().getPassword());
apAuthenticationPage.clickOnSignInButton();
Assert.assertEquals("Authentication failed.", apAuthenticationPage.IsErrorBannerDisplayed());
}
}
I'm suspecting that's something related to the testng.xml file (but, tbh, there are some things that I don't understand about how to configure correctly this file).
I'll appreciate any help to solve my problem. Thanks in advance!
It's not a bug. It work as expected.
BeforeTest
BeforeMethod
Method 1: CreateNewUserWithValidData
BeforeMethod
Method 2: LoginWithAValidUser
BeforeMethod
Method 3: LoginWithAnInvalidUser
AfterTest
If you want to close the browser before method 2, then you need to change AfterTest --> AfterMethod, and initialize browser in BeforeMethod
If you just want to change the testng.xml
<test name="test1">
<classes>
<class name="Tests.AutomationPracticesTests">
<methods>
<include name="CreateNewUserWithValidData"/>
</methods>
</class>
</classes>
</test>
<test name="test2">
<classes>
<class name="Tests.AutomationPracticesTests">
<methods>
<include name="LoginWithAValidUser"/>
</methods>
</class>
</classes>
</test>
<test name="test3">
<classes>
<class name="Tests.AutomationPracticesTests">
<methods>
<include name="LoginWithAnInvalidUser"/>
</methods>
</class>
</classes>
</test>

How can I make this testNG test dynamic but remain parallel

public class FactoryTest {
#Test
#Parameters("Row")
public void run1(int row) throws MalformedURLException{
new Controller(row);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
<test thread-count="2" name="factory test" parallel="methods">
<classes>
<class name="RealPackage.FactoryTest">
<methods>
<include name="run1">
<parameter name="Row" value="1"/>
</include>
</methods></class>
</classes>
</test> <!-- OfficialTestName -->
</suite> <!-- Suite -->
This is an example of one of the tests I need to run. I need it to run in parallel with other tests. So in the test run1() I create a Controller(row) which initiates the test and I pass a row number to it. I want to run new Controller(1) and new Controller(2) and new Controller(3), etc all at the same time. I am able to do this if I change the java file to this:
public class OfficialTest {
#Test
public void run1() throws MalformedURLException{
new Controller(1);
}
#Test
public void run2() throws MalformedURLException{
new Controller(2);
}
#Test
public void run3() throws MalformedURLException{
new Controller(3);
}
#Test
public void run4() throws MalformedURLException{
new Controller(4);
}
#AfterMethod
public void close() {
System.out.println("closing");
}
}
but this isn't dynamic. I need to be able to run this using any range of number for the row. So I was thinking maybe I could generate an XML file that would take care of this but I still am not sure if it would even be able to run in parallel that way.
I was able to fix it with this:
public class ParallelTests
{
int row;
#Parameters({"Row"})
#BeforeMethod()
public void setUp(int rowParam) throws MalformedURLException
{
row = rowParam;
}
#Test
public void RunTest() throws InterruptedException, MalformedURLException
{
new Controller(row);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="5" name="BlogSuite" parallel="tests">
<test name="Test 1">
<parameter name="Row" value="1"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 2">
<parameter name="Row" value="2"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 3">
<parameter name="Row" value="3"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 4">
<parameter name="Row" value="4"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 5">
<parameter name="Row" value="5"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
</suite>

Can a Parameterized method be called in a TestNG XML Suite?

I have a parameterized method(readConfig_1(String path)) in a class which I need to run before the tests in a TestNG Suite. Is there a way that I can call this method and define the parameter for the same in the TestNG.xml file?
Here's the Parameterized Method, I have written which actually needs a path to where the XML file is stored.
public static void readConfig_1(String configXmlPath)
{
browser = CoreLib.fGetNodeText(configXmlPath, "config",
"browser");
env = CoreLib.fGetNodeText(configXmlPath, "config", "env");
release = CoreLib.fGetNodeText(configXmlPath, "config", "release");
serverName = CoreLib.fGetNodeText(configXmlPath, env,
"serverName");
host = CoreLib.fGetNodeText(configXmlPath, env, "host");
userName = CoreLib.fGetNodeText(configXmlPath, env, "userName");
passWord = CoreLib.fGetNodeText(configXmlPath, env, "passWord");
portNumber = CoreLib.fGetNodeText(configXmlPath, env,
"portNumber");
schema = CoreLib.fGetNodeText(configXmlPath, env, "schema");
url = CoreLib.fGetNodeText(configXmlPath, env, "url");
screenShotForPass=CoreLib.fGetNodeText(configXmlPath, env, "SCreenShotForPass");
screenShotForFail=CoreLib.fGetNodeText(configXmlPath, env, "SCreenShotForFail");
CoreLib.LOGGER.info("****************************************************");
CoreLib.LOGGER.info(" Configuration Details ");
CoreLib.LOGGER.info("****************************************************");
CoreLib.LOGGER.info("Browser ::" + browser);
CoreLib.LOGGER.info("env ::" + env);
CoreLib.LOGGER.info("serverName ::" + serverName);
CoreLib.LOGGER.info("host ::" + host);
CoreLib.LOGGER.info("userName ::" + userName);
CoreLib.LOGGER.info("passWord ::" + passWord);
CoreLib.LOGGER.info("portNumber ::" + portNumber);
CoreLib.LOGGER.info("schema ::" + schema);
CoreLib.LOGGER.info("url::" + url);
CoreLib.LOGGER.info("ScreenSnapShotForPass::"+screenShotForPass );
CoreLib.LOGGER.info("ScreenSnapShotForFail::"+screenShotForFail );
}
In this TestNG Suite seen below, I need to call the above method passing a parameter to it before it can go ahead and run the tests written in the other classes.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Smoke Suite" parallel="false" preserve-order="true">
<listeners>
<listener class-name="com.healthcare.reports.MyListener"></listener>
</listeners>
<test name="XYZ Tests">
<classes>
<class name="com.healthcare.utility.Config">
<methods>
<include name="readConfig_1"></include>
</methods>
</class>
<class name="com.healthcare.businessLib.xyz.AddUserTests" />
</classes>
</test>
</suite>
By this I intend to restrict a TestNG Suite to read a particular Config.XML file which will have it's own values such as Env, URL, browser etc., set before the tests can be executed. Is there a way I can achieve this?
----Added 11/24/2017-----
---- I thought adding the readConfig_1 to a #BeforeClass annotation would resolve the problem. But there's more to it-----
My Listener Class has **#onStart** annotation which needs the config file to be run on the start of the Suite. As you see below my Listener Class has the variables release_1 coming from Config file.
public class MyListener implements ITestListener, ISuiteListener {
// This belongs to ISuiteListener and will execute before the Suite start
ReportLib report=new ReportLib();
#Override
public void onStart(ISuite arg0) {
Config.readConfig_1(configXlsPath);
ExportTestResults export = new ExportTestResults();
export.exportExcelHeader(Config.release_1);
CoreLib.fCreateLogger(Config.release_1);
}
But if I put it in #BeforeClass in a TestClass these variables(Config.release_1) are returning null as they would be running before the test class. So I need the readconfig_1 to run before or with the Listener class and unable to add a parameter to the onStart(ISuite arg0).
Tried a few things by :
Running the readConfig_1() in the TestNG.XML as the first method even before the listener class could be called.
putting a #BeforeClass annotation in the Listener class with readConfig_1() method parameterized in it- hoping that the readConfig would be called before the onStart() is executed.
public class MyListener2 implements ITestListener, ISuiteListener {
ReportLib report=new ReportLib();
#BeforeClass
#Parameters("configXlsPath")
public void beforeSuite(String configXlsPath)
{
Config.readConfig_1(configXlsPath);
System.out.println("In Before Class(Listener_2)"+ Config.release_1);
}
#Override
public void onStart(ISuite arg0) {
ExportTestResults export = new ExportTestResults();
System.out.println("In onStart(Listener_2)"+ Config.release_1);
export.exportExcelHeader(Config.release_1);
CoreLib.fCreateLogger(Config.release_1);
}
}
But none worked.
Is there a way around this now?
Thanks in advance.
Yes, you can do this. Firstly, add #Parameters("configXmlPath") annotation to your test class. configXmlPath parameter must also be defined in testng.xml file like <parameter name = "configXmlPath" value="Whateverthevalueis"/> this. Here is an example.
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class YourTest {
#Test
#Parameters("configXmlPath")
public void test1(String configXmlPath) {
System.out.println("Parameterized value is : " + configXmlPath);
}
}
Then, in your testng.xml define parameter like this:
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
<test name = "test1">
<parameter name = "configXmlPath" value="Whateverthevalueis"/>
<classes>
<class name = "YourTest" />
</classes>
</test>
</suite>
In your java method , use #BeforeClass and #Parameters annotation ,
#BeforeClass
#Parameters({"configXlsPath"})
public static void readConfig_1(#Optional("addaDefaultPathValue") String configXlsPath)
In your xml , add a parameter tag after tests tag.
<test name="Purchaser Tests">
<parameter name="configXlsPath" value="target/path/to/xmlFile">
You need to define your Parameterized Method under #BeforeClass annotations and should be inside the test class or inherited from other class. I was also dealing with the same issue and resolved this in the below ways:
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class AddUserTests {
#BeforeClass(alwaysRun = true)
#Parameters("configXmlPath")
public void readConfig_1(String configXmlPath){
System.out.println("File path : "+ configXmlPath);
/*
You can use the configXmlPath value to
your code that goes here
*/
}
#Test
public void test_1(){
//Test Code
}
#Test
public void test_2(){
//Test Code
}
}
You need to define and set your parameter value in the xml file like
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Smoke Suite" parallel="false" preserve-order="true">
<listeners>
<listener class-name="com.healthcare.reports.MyListener"></listener>
</listeners>
<test name="XYZ Tests">
<parameter name="configXmlPath" value="USE_ABSOLUTE_PATH_HERE"/>
<classes>
<class name="com.healthcare.businessLib.xyz.AddUserTests" />
</classes>
</test>
</suite>
Please use the absolute path [i.e,C://Config.XML] of the config file instead of USE_ABSOLUTE_PATH_HERE

Unable to click the link on IE using IEDriver

The code I am using is shown below.
MultiBrowser
package com;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowser {
public WebDriver driver;
// Passing Browser parameter from TestNG xml
#Parameters("browser")
#BeforeClass
public void beforeTest(String browser) {
// If the browser is Firefox, then do this
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
}else if (browser.equalsIgnoreCase("ie")) {
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
File fil = new File("C:\\IEDriver\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", fil.getAbsolutePath());
driver = new InternetExplorerDriver(capabilities);
}
driver.get("http://www.store.demoqa.com");
}
#Test
public void login() throws InterruptedException{
driver.findElement(By.xpath(".//*[#id='account']/a")).click();
driver.findElement(By.id("log")).sendKeys("testuser_1");
driver.findElement(By.id("pwd")).sendKeys("Test#123");
driver.findElement(By.id("login")).click();
}
#AfterClass
public void afterTest(){
driver.quit();
}
}
TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="com.MultiBrowser" />
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="ie" />
<classes>
<class name="com.MultiBrowser" />
</classes>
</test>
</suite>
It works fine when I use Firefox but I get the below issue when I run the same code on IE
Exception :
Unable to find element with xpath == .//*[#id='account']/a (WARNING: The server did not provide any stacktrace information)
It needs to set Security level in all zones. To do that follow the steps below:
1.Open IE
2.Go to Tools -> Internet Options -> Security
3.All check boxes in Security should be enabled.

Categories