- 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.
- 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. ****************************** |
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);
- }
- }
- 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);
- }
- }
- 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);
- }
- }
- 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();
- }
- }
- 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();
- }
- }
User Stories: |
1. Open Chrome Browser |
2. Navigate to the Testingcolleges blogger 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 quit() |
- 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();
- }
- }
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