Saturday, September 14, 2019

How to invoke Chrome browser using selenium WebDriver? | Selenium Interview Questions



import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeInvocation {

       public static void main(String[] args) {
             
              System.setProperty("webdriver.chrome.driver", "Driver\\chromedriver.exe");
              WebDriver driver = new ChromeDriver();
              driver.get("https:\\www.google.com");
       }

}

Explanation:
  • System.setProperty() – set the location of the executable selenium webdriver
  • WebDriver driver = new ChromeDriver(); - creates a chrome driver object with WebDriver reference
  • driver.get() – loads the URL in the chrome browser


        I.            What if we don’t provide System.setProperty()?

public class ChromeInvocation {

       public static void main(String[] args) {

              WebDriver driver = new ChromeDriver();
              driver.get("https:\\www.google.com");
       }

}

Output:
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;

      II.            What if we provide an incorrect driver path?

public class ChromeInvocation {

       public static void main(String[] args) {
             
              System.setProperty("webdriver.chrome.driver", "Driver\\chromedrier.exe");
              WebDriver driver = new ChromeDriver();
              driver.get("https:\\www.google.com");
       }

}

Output:
Exception in thread "main" java.lang.IllegalStateException: The driver executable does not exist: C:\Users\hp\eclipse-workspace\SeleniumJava\Driver\chromedrier.exe

    III.            What if we provide ‘webdriver.chome.driver’?

public class ChromeInvocation {

       public static void main(String[] args) {
             
              System.setProperty("webdriver.chome.driver", "Driver\\chromedriver.exe");
              WebDriver driver = new ChromeDriver();
              driver.get("https:\\www.google.com");
       }

   }

   Output:

   Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;

   IV.            What if we provide an incorrect URL?

public class ChromeInvocation {

       public static void main(String[] args) {
             
              System.setProperty("webdriver.chrome.driver", "Driver\\chromedriver.exe");
              WebDriver driver = new ChromeDriver();
              driver.get("https:\\www.e.com");
       }

}

Output:



        V. What if we don’t write https?

public class ChromeInvocation {

       public static void main(String[] args) {
             
              System.setProperty("webdriver.chrome.driver", "Driver\\chromedriver.exe");
              WebDriver driver = new ChromeDriver();
              driver.get("www.google.com");
       }

}

Output:

       Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument

      VI.  When we will get SessionNotCreatedException?

             We the browser and webdriver API version is not compatible.
             For e.g. Chrome browser version is 75 but the browser driver supports version 70

No comments:

Post a Comment