Batch files launches and disappear without executing Selenium tests - java

I have created a Selenium project named 'LearnAutomation' in Java and convert it to TestNG tests which created a testng.xml for me. Now i want to run it via batch file. So i created one as below -
cd E:\Workspace - Eclipse\LearnAutomation
set ProjectPath=E:\Workspace - Eclipse\LearnAutomation
echo %ProjectPath%
set classpath=%ProjectPath%\bin;%ProjectPath%\lib\*
echo %classpath%
java org.testng.TestNG %ProjectPath%\testng.xml
and save it as testng.bat file. On double clicking batch file, it gets launched and disappeared. (failed to run test)
My testng.xml looks like this -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="tests.Demo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
My Demo class looks like this -
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Demo {
#Test
public void batchFile() {
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\srvashishtha\\Downloads\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}

Related

Why is my TestNG xml file not targeting my Java Class?

Why is my TestNG xml file not targeting my Runner Class?
My XML File:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Cucumber Framework" verbose="2">
<test name="Cucumber Tests">
<classes>
<class name="runners.MainRunner"></class>
</classes>
</test>
</suite>
location of the runner class:
CucumberFramework\src\test\java\CucumberFramework\runners
CucumberFramework\src\test\java\CucumberFramework\runners\MainRunner.java
Please note: if i execute the runner class directly, everything works accordingly, it seems the xml file is unable to locate the runner class, any ideas?
Runner class code:
package CucumberFramework.runners;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions (
features = {"src/test/java/CucumberFramework/featureFiles/"},
glue = {"CucumberFramework.steps"},
monochrome = true,
tags = {},
plugin = {"pretty", "html:target/cucumber","json:target/cucumber.json", "com.cucumber.listener.ExtentCucumberFormatter:target/report.html"}
)
public class MainRunner {
}
You can execute the junit classes with testng xml file by define the property junit="true" as given below
<suite name="Cucumber Framework" >
<test name="Cucumber Tests" junit="true">
<classes>
<class name="CucumberFramework.runners.MainRunner" />
</classes>
</test>
</suite>

Testing browsers simultaneously; receiving error

Right now, I'm trying to work on my first test that will test browsers Chrome, Firefox, IE, and Safari in parallel. But the following error that I'm getting is this:
FAILED CONFIGURATION: #BeforeMethod beforeMethod
org.testng.TestNGException:
Parameter 'browser' is required by #Configuration on method beforeMethod but >has not been marked #Optional or defined
I'm using Selenium, TestNG and Maven with the JAVA language. The XML test suite file and the java file are in the same folder in the directory. What I was able to find for the test suite XML file online is as follows (with class name values set to the correct package and class name):
<?xml version="1.0" encoding="UTF-8"?>
<DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="2" parallel="tests">
<test name="ChromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="ie" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
<test name="SafariTest">
<parameter name="browser" value="safari" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
</suite>
Below is my code to just open the browser to make sure that it will run and pass:
package com.sqa.ts.multiBrowser;
import java.net.MalformedURLException;
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.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class BrowserTest {
private WebDriver driver;
#Test
public void testCaseOne() {
driver.get("http://www.google.com");
driver.close();
}
#BeforeMethod
#Parameters("browser")
public void beforeMethod(String browser) throws MalformedURLException {
if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:/Users/Trevor/workspace/BrowserTest/drivers/chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver", "C:/Users/Trevor/workspace/BrowserTest/drivers/IEDriverServer.exe");
driver = new InternetExplorerDriver();
} else if (browser.equalsIgnoreCase("safari")) {
driver = new SafariDriver();
}
}
#AfterMethod
public void afterMethod() {
driver.quit();
}
}
If anyone can give me an insight as to what is causing this issue, I would greatly appreciate it. Thank you.
Looks like you are missing the testNG configuration in the pom.xml
<build>
<plugins>
<!-- Following plugin executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>src/test/java/com/sqa/ts/multiBrowser/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</build>
Running mvn clean install or mvn clean install should run the test cases. Hope this helps you.
It looks like executing the XML file as Run as > Testng Suite. That was the fix to my problem.
It worked with option = Run as > Testng Suite

How to use a test class from a jar in TestNG

I have a JAR(xyz.jar) with test class test.sample.ClassX. I am trying to use this class in testng.xml shown below,
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SuiteX" verbose="1" >
<test name="TestX" >
<classes>
<class name="test.sample.ClassX" />
</classes>
</test>
</suite>
My pom.xml has dependency(xyz.jar) added. I can execute this suite from eclipse but fails when i try to execute from command-line(mac) with below error,
error: package test.sample does not exist
Any idea what the issue might be ?
My first guess is that you might not have a class defined like:
package test.sample;
public class TestX
{
...

TestNG runs no test from console

I installed NetBeans 8.0.1 on my computer and used it to create a test suite :
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testing">
<test name="suite">
<packages>
<package name="myPackage.tests"/>
</packages>
</test>
</suite>
In myPackage.tests there is a test class, a very simple one :
[package, imports]
public class Tests001 {
public Tests001 () {
}
#Test
public void Fail() {
assertFalse(true);
}
In NetBeans, I launched the test by right-clicking the.xml file and selecting 'test file'. Everything works perfectly - or rather, it correctly fails - intended result for now.
But I need to launch it from console, so I run :
> java org.testng.TestNG testing.xml
And get :
===============================================
testing
Total tests run: 0, Failures: 0, Skips: 0
===============================================
No test is run. I can't find out why : test class and methods are public ; java is configured correctly (JDK 1.8.0_25) and I set CLASSPATH to
C:\Program Files\NetBeans 8.0.1\platform\modules\ext\testng-6.8.1-dist.jar
I must have forgotten something...
Or is there a way, from console, to ask NetBeans to proceed to test ?
In my experience, the suite.xml file needs this structure:
<suite name="mySuite" allow-return-values="true" parallel="tests"
verbose="1" thread-count="2">
<test name="TestStuff">
<groups>
<run>
<include name="Smoke.mytests"/>
</run>
</groups>
<packages>
<package name="com.myco.mytests"/>
</packages>
</test>
</suite>

error could not find or load main class -eclipse?

Currently working on selenium webdriver and using java.
I have created a java project as OneReports and inside the project i have many java files as shown in the screenshot
I'm trying to run the java file through command prompt and getting error as follows:
C:\Program Files\Eclipse\eclipse>java LoginOneReports.java
Error: Could not find or load main class LoginOneReports.java
At the same time i want to create a task scheduler for the Test.xml. The xml file contains three java files as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestAll">
<test name="test1">
<classes>
<class name="test.LoginOneReports" />
</classes>
</test>
<test name="test2">
<classes>
<class name="test.OEPR_DefaultTab" />
</classes>
</test>
<test name="test3">
<classes>
<class name="test.OEPR_InternalvsExternalTab" />
</classes>
</test>
</suite>
I have created a run.bat as follows:
#echo off
set ProjectPath=C:\Documents and Settings\amth\workspace\OneReports\src\
echo %ProjectPath%
set PATH=%ProjectPath%bin;%ProjectPath%lib*
set path=%PATH%%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files (x86)\Java\jdk1.7.0_51\bin;
echo %PATH%
pause
echo java org.testng.TestNG %ProjectPath%Test.xml
I'm getting the error as error could not find or load main class. Please any one can help immediately.
From command line run the below commands one by one
cd C:\Documents and Settings\amth\workspace\OneReports\src\test
javac LoginOneReports.java
java -cp . LoginOneReports
You should study the docs on how to run the java program from command line
http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
http://www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/

Categories