Selenium Web Driver–First Test Script

Selenium Web Driver–First Test Script

In these tutorials, we are learning how to write a test script in selenium web driver.

In this test case, we will automate the following test scenarios:

Then, right-click on the src folder and create a new Class File from New ? Class.

Open the Eclipse IDE and the existing test suite new_test, which we have created in the WebDriver Installation section of the WebDriver tutorial.

Selenium Web Driver–First Test Script 1
  • Give the Class name as First_test and click onthe Finish button.
Selenium Web Driver–First Test Script 2

We are creating our sample test script step by step to give you a complete understanding that how we write a test script in selenium web driver,

Below are the following steps,

Steps Action Method Used Input Excepted Result
1. Open the Google Chrome Browser. System.setProperty()   The Google Chrome browser must be opened.
2. Navigate to the URL. get() https://www.tutorialandexample.com The home page window must be displayed.
3. Maximize the browser window and delete all the cookies.     The browser window should be maximized, and cookies should be deleted.
4. Capture the current title and current URL of the tutorialandexample.com The current title and the current URL of the page should be captured.
5. Close the browser window. close()   The browser window should be closed.

Note: To access a web browser in Selenium, we have to download an executable file which is specific to that browser.

Step1:

  • To access the Google Chrome browser. First, we need to download Google Chrome driver and set the system property for Chrome driver.
  • We have already discussed this in previous sessions of the tutorial.
  • And you can also refer given link "Working with Chrome browser” for a complete understanding that how we download it and set System property for Chrome driver.

Note: Selenium developers have defined properties for each browser that needs the location of the corresponding executable files to be assigned to access the browser.

The Syntax for set property of the browser:
System.setProperty (“key”, “value”); 

Where,

  Key = webdriver.chrome.driver”

 Value = "C:\\Users\\JTP\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe"

Here the sample code,

// set the system property for chrome browser 
System.setProperty("webdriver.chrome.driver", "C:\\Users\\JTP\\Downloads\\chromedriver_win32\\chromedriver.exe"); //create the object for chrome driver WebDriver driver = new ChromeDriver();   

Step2:

After that, we will write the code to automate our second step, which navigates to the given URL.

The sample code for the navigation of the desired URL:

//navigate to the URL
driver.get("https://www.tutorialandexample.com"); 

Step3:

Now, we will maximize the window size and delete all the cookies.

Here the sample code,

/maximize the window size
driver.manage().window().maximize(); 
//delete all the cookies
driver.manage().deleteAllCookies(); 

Step4:

After that, we will capture the current title and current URL of the tutorialandexample.com.

Here the sample code,

//get the current page title
System.out.println(driver.getTitle());
//get the current page URL
System.out.println(driver.getCurrentUrl());                         

After completing all the steps successfully, our final text script will look like this,

package testpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class First_test {
public static void main(String[] args) {
// System Property for CHROME Driver   
System.setProperty("webdriver.chrome.driver", "C:\\Users\\JTP\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
//create driver class object
WebDriver driver = new ChromeDriver();
//maximize he window size
driver.manage().window().maximize(); 
//delete all the cookies
driver.manage().deleteAllCookies();
//navigate to the URL
driver.get("https://www.tutorialandexample.com");
//get the current page title
System.out.println(driver.getTitle());
//get the current page URL
System.out.println(driver.getCurrentUrl());
//close the browser
driver.close();
}
} 

To run the test script in Eclipse IDE, we have to right-click on the code and then select Run As ? Java Application.

Selenium Web Driver–First Test Script 3

The above test script will be launched in the Google Chrome browser, and automate all the test scenarios which we performed above.

The output of the test script is displayed in the Google Chrome browser with a message, i.e.

Chrome is being controlled by automated test software.”

which is as shown below,

Selenium Web Driver–First Test Script 4

And, for these two methods, get current title and get current URL, the output is shown in the below snapshot:

Selenium Web Driver–First Test Script 5

Summary of the Code:

  • Import Packages/Statements:

Import statements = to import the classes present in another package.

 Import keyword = to import built-in and user-defined packages into your java source file.

  • org.openqa.selenium.WebDriver :

It is used to instantiate a new web browser in the WebDriver interface.

  • org.openqa.selenium.chrome.ChromeDriver :

It is used to instantiate a Chrome-specific driver into the browser instantiated by the WebDriver class.

  • Instantiating objects and variables:

Creating a driver object for chrome browser:

WebDriver driver=new ChromeDriver();  

Whenever an object is created or at the time of object creation, an empty browser will be launched because all the browser classes are default constructor.

Whenever the driver launches the browser, all the add-ons history and cookies will be disabled automatically.

The Web driver cannot run the program with an existing or already opened browser.

The Web driver always launches a new browser for each execution.

Web driver has some in-build browser control methods in selenium.

  • Get the current page title-getTitle():

This method is used to capture the title of the web page.

 e.g., System.out.println(driver.getTitle());                        

  • Get the current page URL-getCurrentUrl():

This method is used to capture the current URL of the current page.

e.g., System.out.println (driver.getCurrentUrl());

  • Launch the website:

To open or launch the application, we will use the get() method in selenium web driver.

get():- get() method is used to navigate to any URL.

driver.get("https://www.tutorialandexample.com");