Showing posts with label Selenium Exceptions. Show all posts
Showing posts with label Selenium Exceptions. Show all posts

Run Selenium Webdriver Scripts using Command Line

Welcome to Selenium Tutorials, today we will learn Run Selenium Webdriver Scripts using Command Line with examples.After creating Selenium script in Eclipse,you can run easily
but it is not recommended every time because in case you want to run on client machine then it is recommended to use command prompt instead of opening eclipse and right click and run.




Before executing Selenium Test Scripts from command line then you should've selenium setup in your machine, still not done then follow the below link and complete the setup.

Selenium Webdriver Setup

Once you start designing Selenium Test cases Scripting continuously your Test Cases count will increase, so you've to manage your test cases accordingly. TestNG framework providing very helpful concept of creating testng.xml file based on requirement and put all relevant test classes inside the testng.xml file and run it as a Test Suite.

Trending Posts:

Write TestCase PASS/FAIL In Excel using Selenium
Solution - Testng Cannot Find ClassPath Error
Generate HTML Reports using Selenium
Webdriver Instance to Multiple Classes

Pre Requisites-

1- TestNG should be installed inEclipse.
2- testng xml file should be created.
3- Class-path should be set.

Run Selenium Webdriver Scripts using Command Line

Src - the .java files can be found here
Bin - the .class files will be generated here

Step 1- Create testng.xml files and store all xml into project home directory as given below.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Selenium Test Suite">
    <listeners>
        <listener class-name="org.uncommons.reportng.HTMLReporter"/>
        <listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>       
    </listeners>
  <test name="SeleniumRegressionTest">
    <classes>
      <class name="com.sel.Selenium.Practise.GoogleSite"/>
    </classes>
  </test>
</suite>



Step 2- Set classpath now

Important points to remember-

1-Put all the jars (Selenium jars) into separate folder and  put that folder into project home directory.

I've created Lib folder and pasted all required jars inside a folder ,downloaded from Selenium website.

Refer below screenshot of Lib folder with required jar files.

2- Open command prompt and type E: and press enter because my project work Space in E drive.


Now go to project home directory for this type directory path and enter as below


Run Selenium Webdriver Scripts using Command Line

Now we can set classpath for this,specify bin folder.

Home Directory > set classpath=Home Directory\bin; and press enter

Specify lib folder where all jars is available

Home Directory > set classpath=Home Directory\lib\*; and press enter

Step 3-Now run xml file using below command

Home-directory > java org,testng.TestNG testng1.xml and hit enter




Other wise you can run in single step as below command.

Go to Project Folder path in Command Line.
Enter below command in command line and hit enter.


java -cp E:\QTPTutorials\Selenium_Workspace\Selenium_Tutorials\Lib\*;E:\QTPTutorials\Selenium_Workspace\Selenium_Tutorials\bin org.testng.TestNG testng.xml



Test Results:

Run Selenium Webdriver Scripts using Command Line
Test Results


Thanks for visiting my blog. If you find this post informative then please share with your friends and provide your valuable comments on this post.

Java Lang NullPointerException in Selenium

Java Lang NullPointerException in Selenium,While running the selenium test scripts you'll face some challenges with the test scripts,in that challenges this is the main important exception,not able to understand why it is displaying and most of the time you'll think that it is raised because of element is not identifying by the Selenium,that is not the proper answer.

Java Lang NullPointerException in Selenium

Java Lang NullPointerException in Selenium


Also Read:


Write TestCase is PASS/FAIL In seleniumTestNG Classpath errorSession ID is null using webdriver after calling quitHow to send extent reports in email with screenshots





Java Lang NullPointerException in Selenium

When coming to Java Lang NullPointerException in Selenium,when passing single webdriver instance to multiple classes,you'll face this type of problems.Please read below instructions  to resolve the same issue in Selenium.

SelDriver Class:



package com.sel.Selenium.Practise;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;

public class SelDriver {  
 public WebDriver driver;
 String baseurl = "http://www.google.co.in";  
 
 @BeforeSuite
 public void openBrowser(){
  
  driver = new FirefoxDriver();
  //driver = new HtmlUnitDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
  
 } 
}

GoogleSite Class:



package com.sel.Selenium.Practise;
import org.testng.Assert;
import org.testng.annotations.Test; 
public class GoogleSite extends SelDriver {
 
 @Test
 public void seleniumElements() throws InterruptedException{
  
  driver.get(baseurl);
  System.out.println("Open Google site");
  
  String Title=driver.getTitle();
  Assert.assertEquals(Title, "Google");
  
  try {
        Assert.assertEquals(driver.getTitle(), "Google's");
      } catch (Error e) {
        System.out.println(e.getCause());
      }   
 } 
}

I', extending the class from SelDriver main class to use WebDriver instance,But when running the above class,you'll face same issue i.e Java.Lang.NullPointerException for some methods.In order to resolve this issue,you need to add static for WebDriver declaration as below.



package com.sel.Selenium.Practise;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;

public class SelDriver {
 
 public static WebDriver driver;
 String baseurl = "http://www.google.co.in";  
 
 @BeforeSuite
 public void openBrowser(){
  
  driver = new FirefoxDriver();
  //driver = new HtmlUnitDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
  
 } 
}


Before Static Method Test Results:




After Changes - Test Results

[TestNG] Running:
  E:\QTPTutorials\Selenium_Workspace\Selenium_Tutorials\testng.xml

Open Google site
null
driver=FirefoxDriver: firefox on WINDOWS (f2f86423-9806-4b4a-ae92-7504e3db59be)
Enter Search Keyword
Clear Enter Keyword
clear works

===============================================
GoogleTestSuite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

TestNG Test Results


Please provide your valuable comments on this solution in case it is resolved your java.lang.nullpointer exception in Selenium.