What is WebDriver?
- WebDriver is an interface provided by Selenium WebDriver.
- Interface is a collection of abstract methods(Methods without implementation) (Like close(), findElement(), get(), getTitle(), quit(), switchTo(), …….. )
- WebDriver interface acts as a contract that each browser-specific driver implements.
Note: By using Selenium we can only test the
Web Based Applications.
How to Launch Chrome browser?
To Launch the web browser we need to
create an object for the ChromeDriver Contactor, Using the
WebDriver interface we can launch the web browser.
Example: WebDriver driver = new ChromeDriver();
Here we have created a ChromeDriver instance and stored it
in a variable called driver, which is of type ‘WebDriver’ interface.
Note:
- For the Firefox browser "WebDriver driver = new FirefoxDriver();"
- For the Edge browser "WebDriver driver = new EdgeDriver();"
- For the Opera browser "WebDriver driver = new OperaDriver();"
- For the Safari browser "WebDriver driver = new SafariDriver();"
Interview Question: Explain this? WebDriver driver = new ChromeDriver(); We can create an Object of a class ChromeDriver by taking reference of an interface (WebDriver). In this case, we can call the implemented method of the webDriver interface. ****************************** |
Browser Commands:
get():
get method
is used to launch the website.
Syntax: driver.get(“url”);
Syntax: driver.get(“url”);
- Example Program:
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
-
- public class GetMtd {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://testingcolleges.blogspot.com/");
- }
- }
getTitle(): getTitle method is used to fetch the title of the current webpage.
Syntax: driver.getTitle();
- Example Program:
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class GetTitle {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://testingcolleges.blogspot.com/");
- String title = driver.getTitle();
- System.out.println(title);
- }
- }
getCurrentUrl(): get Current
Url method is used to get the URL of the active browser window.
Syntax: driver.getCurrentUrl();
- Example Program:
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class GetUrl {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://testingcolleges.blogspot.com/");
- String curUrl = driver.getCurrentUrl();
- System.out.println(curUrl);
- }
- }
getPageSource(): The get Page Source method is used to get the current web page
source code.
Syntax: driver.getPageSource();
- Example Program:
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class GetPageSrc {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://testingcolleges.blogspot.com/");
- String getSrc = driver.getPageSource();
- System.out.println(getSrc);
- }
- }
close(): The close method is used to close the active window in the
browser.
Syntax: driver.close();
- Example Program:
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class closeMtd {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://testingcolleges.blogspot.com/");
- driver.close();
- }
- }
quit(): The quit method is used to close all windows in the browser.
Syntax: driver.quite();
- Example Program:
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class quitMtd {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://testingcolleges.blogspot.com/");
- driver.quit();
- }
- }
What is the difference between the close() and quit() methods in
selenium?
quit() method turning off the entire
browser and all its tabs. It's like closing Chrome entirely.
close() method closing just one tab in
the browser. If you have multiple tabs open, it will close the one
you're looking at, but the other tabs stay open.
So, quit() ends everything, and
close() only closes what you're looking at,
Example Program 1:
User Stories: |
1. Open Chrome Browser. |
2. Navigate to the TestingColleges Website HomePage. |
3. Get the page Title, page URL and source code of the page. |
4. Print the Title, URL and Source code of the webpage in the Console. |
5. Close the browser window using the quit() method. |
- Program:
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class Example1 {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.get("https://testingcolleges.blogspot.com/");
- String pgTitle = driver.getTitle();
- String pgUrl = driver.getCurrentUrl();
- String pgSrc = driver.getPageSource();
- System.out.println(pgTitle+"\n"+pgUrl);
- System.out.println(pgSrc);
- driver.quit();
- }
- }
Example Program 2:
Steps | Step Actions | Expected Results |
---|---|---|
1 | Open Chrome Browser | Chrome browser should be Launch |
2 | Launch the URL, https://testingcolleges.blogspot.com/ | Web page should be displayed |
3 | Verify the Current URL | https://testingcolleges.blogspot.com/ should be displayed |
4 | Verify the Title of the page | “Testing Colleges” title should be displayed |
5 | Close the browser window | Browser should be Closed |
- Example 2 program:
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- public class Example2Detail {
- public static void main(String[] args) {
- WebDriver driver = new ChromeDriver();
- driver.manage().window().maximize();
-
- //Launch the URL
- String url = "https://testingcolleges.blogspot.com/";
- driver.get(url);
-
- //verify the URL of the current Web page
- String actualURL = driver.getCurrentUrl();
- String expectedURL = "https://testingcolleges.blogspot.com/";
- if(actualURL.equals(expectedURL)) {
- System.out.println("URL of the page :"+ expectedURL +" :Test is Passed");
- }
- else {
- System.out.println("The actual URL of the page is :"+ actualURL + " :Test is Failed");
- }
-
- //Verify the Title of the current Web page
- String actualTitle = driver.getTitle();
- String expectedTitle = "Testing Colleges";
- if(actualTitle.equals(expectedTitle)) {
- System.out.println("Title of the page :"+ expectedTitle +" :Test is Passed");
- }
- else {
- System.out.println("The actual title of the page is :"+ actualTitle +" :Test is Failed");
- }
-
- //Close the Browser
- driver.close();
- }
- }
No comments:
Post a Comment