Selenium Webdriver create project jar - java

I need to run my project in other system(computer2) also through cmd what requirement need in (Computer2).
System.setProperty("webdriver.chrome.driver","Project1\\Driver\\chromedriver.exe);
driver=new ChromeDriver();
driver.get("http://www.gmail.net/");
driver.close();
These are the code and contain main method also
public class Mainpage
{
#SuppressWarnings("deprecation")
public static void main(String[] args) {
TestListenerAdapter t2=new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[]{login.class});
testng.addListener(t2);
testng.run();
}
}

It's unclear what error you're getting (or if you're even getting an error), but changing "http://www.gmail.net/" to "http://www.gmail.com/" might solve your issue.

Related

Intellij IDE throws error while trying to use Extent Report in Java, Selenium, TestNG environment

I am trying to add ExtentReport in my test automation project, but IntelliJ is throwing this error at line "report = ..." inside "BeforeMethod"
Expected 0 arguments but found 1
I am trying to insert the location of the report file. Help much appreciated. Thanks.
public class Methods {
public static WebDriver driver;
public static ExtentReports report;
public static ExtentTest test;
#BeforeTest
public void startReport(){
report = new ExtentReports(System.getProperty("C:\\Program Files (J)\\VCWebMaine\\Report.html"));
}
#BeforeMethod
public void setDriver() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (J)\\VCWebMaine\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://xxxxxxxxxxxxxxxx");
Thread.sleep(2000);
}
I tried looking for answers in Google. It looks like the same lines work in Eclipse. Is it a problem in the IDE?
Other part of the script as below:
public class TestCases extends Methods {
ExtentTest test = Methods.test;
#Test
public void scenario01() throws InterruptedException, IOException {

how to make driver as thread safe to run methods of a class in parallel in selenium java

I have 3 test methods using driver from Base class. I tired to run these methods in parallel but getting failures. Reponse to my problem is appreciated. Thanks
Class having 3 test methods
public class TestCases extends BaseClass {
#Test
public void Test1() {
homePage.checkIfElementIsDisplayed(homePage.emailElement);
homePage.checkIfElementIsDisplayed(homePage.passwordElement);
homePage.checkIfElementIsDisplayed(homePage.signInElement);
homePage.emailElement.sendKeys("karteek#gmail.com");
homePage.passwordElement.sendKeys("******");
}
#Test
public void Test2() {
homePage.checkValuesInListGroup();
homePage.checkSecondListItem();
homePage.checkSecondListItemBadgeValue();
}
#Test
public void Test3() throws InterruptedException {
homePage.ScrolltotheElement(homePage.dropDownOption);
homePage.checkDefaultSelectedValue();
homePage.selectOption3();
}
}
Base Class
public class BaseClass {
public WebDriver driver;
public HomePage homePage;
public WebDriver setup() throws IOException {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(
System.getProperty("user.dir") + "\\src\\main\\resource\\GlobalData.Properties");
prop.load(fis);
String browserName = System.getProperty("browser") != null ? System.getProperty("browser")
: prop.getProperty("browser");
if (browserName.contains("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
else if (browserName.contains("edge")) {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
} else if (browserName.contains("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
return driver;
}
#BeforeMethod
public HomePage LaunchApplication() throws IOException {
driver = setup();
homePage = new HomePage(driver);
homePage.goTo();
return homePage;
}
#AfterMethod
public void tearDown() throws IOException {
driver.close();
}
I tried creating ThreadLocal Class for WebDriver as
ThreadLocal<WebDriver> threadSafeDriver=new ThreadLocal<WebDriver>();
and use this in setup() method of BaseClass by writing
threadSafeDriver.set(driver);
but this didnot really help
Most likely you are using the TestNG framework. One of the differences between JUnit and TestNG is that JUnit creates a new class instance for each test method by default but TestNG creates a single instance for all test methods in the class.
You can see the parallel option in TestNG suite (see docs) but there is no way to force TestNG to create a new instance for each test.
The simplest solution is to switch to JUnit framework. Then the code from the example should work.

What is my program in Selenium not stopping executing?

I built my Selenium test cases in Maven, and the following in my main method:
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "webdriver/chromedriver");
WebDriver driver=new ChromeDriver();
driver.get("https://localhost:4502");
driver.quit();
}
My chromedriver was downloaded and moved to a folder called "webdriver" in the project. Once the program is lauched, the chrome browser is opened and then closed.
However, even after the chrome browser is closed, and "driver.quit()" is executed, why is the not exit and terminates its execution?
public static void main(String[] args) {
String folder_path = System.getProperty("user.dir");
System.out.println(folder_path);
System.setProperty("webdriver.chrome.driver",
folder_path+"\\driver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.google.com");
driver.quit();
}
In your example above you should get compile time exception instead of running session.
I still don't know why it is happening. But, I can use System.exit(1) to exit the program.

#beforetest testng its being ignored for some reason

I am running my Cucumber suite tests with TestNG (Selenium + Java) and getting java.lang.NullPointerException.
I realized the problem is that my #BeforeTest() is being ignored for some reason causing the NullPointer problem.
I am using the TestNG 7.0.0 (but tried to use latest Beta also).
#BeforeTest()
public void setUp() {
driver = Web.createChrome(); // it call a method that has the Chromedriver
}
Web.java
public class Web {
public static WebDriver createChrome() {
System.setProperty("webdriver.chrome.driver", webdriver_path);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://the-internet.herokuapp.com/");
return driver;
}
}
Output
java.lang.NullPointerException
at br.dsanders.steps.steps.accessing_the_Tnternet_herokuapp_com_website(steps.java:62)
at ?.Given accessing the Tnternet.herokuapp.com website(testing.feature:9)
Try like below:
public class steps {
WebDriver driver = null;
public steps() {
this.driver=Web.createChrome();
}
#BeforeMethod()
public void setUp() {
driver.get("http://the-internet.herokuapp.com/");
}
}
Note ClassName here is steps if you have other class name then change the class name and constructor name.
Change the #BeforeTest to #BeforeMethod
Source:
What is the difference between BeforeTest and BeforeMethod in TestNG

Create TestNG.xml file dynamically and pass parameter

I have to execute test scripts using dynamic testng.xml file which means I have to create testng.xml file thru code and pass the parameters to the #Test methods pro grammatically.
For that I have created two Java files DynamicTestNG.java which should generate testng.xml file and run SampleClass.java where the #Test method has been written along with the parameters.
DynamicTestNG.java
public class DynamicTestNG {
public void runTestNGTest(Map<String,String> testngParams) {
//Create an instance on TestNG
TestNG myTestNG = new TestNG();
//Create an instance of XML Suite and assign a name for it.
XmlSuite mySuite = new XmlSuite();
mySuite.setName("MySuite");
//Create an instance of XmlTest and assign a name for it.
XmlTest myTest = new XmlTest(mySuite);
myTest.setName("MyTest");
//Add any parameters that you want to set to the Test.
myTest.setParameters(testngParams);
//Create a list which can contain the classes that you want to run.
List<XmlClass> myClasses = new ArrayList<XmlClass> ();
myClasses.add(new XmlClass("SampleClass"));
//Assign that to the XmlTest Object created earlier.
myTest.setXmlClasses(myClasses);
//Create a list of XmlTests and add the Xmltest you created earlier to it.
List<XmlTest> myTests = new ArrayList<XmlTest>();
myTests.add(myTest);
//add the list of tests to your Suite.
mySuite.setTests(myTests);
//Add the suite to the list of suites.
List<XmlSuite> mySuites = new ArrayList<XmlSuite>();
mySuites.add(mySuite);
//Set the list of Suites to the testNG object you created earlier.
myTestNG.setXmlSuites(mySuites);
TestListenerAdapter tla = new TestListenerAdapter();
myTestNG.addListener(tla);
//invoke run() - this will run your class.
myTestNG.run();
}
public static void main (String args[])
{
DynamicTestNG dt = new DynamicTestNG();
//This Map can hold your testng Parameters.
Map<String,String> testngParams = new HashMap<String,String> ();
testngParams.put("searchtext1", "testdata1");
testngParams.put("searchtext2", "testdata2");
dt.runTestNGTest(testngParams);
}
}
And SampleClass.java
public class SampleClass {
private WebDriver driver;
#BeforeTest
public void setUp()
{
System.setProperty("webdriver.chrome.driver","C:\\Users\\AK5040691\\Desktop\\IE driver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("http://executeautomation.com/blog/custom-testng-library-for-appium/#more-1562");
}
//#Parameters({"searchText1","searchText2"})
//#Test
public void searchText(String text1, String text2)
{
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.className("search-field")).sendKeys(text1);
driver.findElement(By.className("search-field")).clear();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.className("search-field")).sendKeys(text2);
}
}
Its not running. Please let me know the mistake here.
You have to uncomment the #Test annotation in your SampleClass file. And if your SampleClass is in a package , then absolute package name + class name is to be specified in this statement.
myClasses.add(new XmlClass("com.some.package.SampleClass"));
Generally TestNG classes have a suffix or prefix labelled "Test" so that surefire plugin can include them in the execution flow, in case if you are using maven.
You can instead use the constructor with parameter of class object .
myClasses.add(new XmlClass(SampleClass.class));

Categories

Resources