Friday, November 12, 2021

Create first test case in Selenium

We going to create the Frist Test case in Selenium Webdriver. following steps to create a Test case.


Required: 

  • Selenium Jar files,  

  • ChromeDriver


Step1:  Initialize Webdriver


System.setProperty java system property is a key-value pair. Each browser has its own WebDriver implementation. Selenium to interact with the browser-specific driver we need to specify the driver and path of the driver.



WebDriver is the Interface which contains all the Selenium method signatures like findElement(), switchTo(), get() etc.

the driver is a reference variable, ChromeDriver() is a Constructor, new is a keyword and new ChromeDriver() is an Object. 


Step2: Launch Webpage using WebDriver


  • get(): method is used WebDriver will wait until the page has fully loaded. It doesn't maintain the browser History and cookies. we can't use forward and backward.

  • navigate(): method is used to navigate to a particular web page. does not wait to page fully load. It maintains browser history and cookies. 


We can use navigation between the pages back, forward and refresh.


3.  Close Webdriver


  • Close(): method is used to close the currently open window on which driver has focus on. 

  • Quit(): This method is used to destroy the instance of WebDriver. It closes all browser windows associated with that driver and safely ends the session.


Sample Code:

 

package DemoTestCase;

import org.openqa.selenium.By;

public class FirstTestCase {
	
	public static void main(String args[]) {
	      
	    System.setProperty("webdriver.chrome.driver", "C:\\Users\\RMJ\\eclipse-workspace\\SeleniumFirstTestCase\\Drivers\\chromedriver.exe");  
	    WebDriver driver=new ChromeDriver();  
	      
	  
	    driver.navigate().to("http://www.google.com/");  
	            
	    driver.findElement(By.id("lst-ib")).sendKeys("javatpoint tutorials");  
	            
	    driver.findElement(By.name("btnK")).click();  
	}

}