Tuesday, September 3, 2019

How to open browsers without driver files in selenium | Selenium Automation


How to open browsers without driver files in selenium | Selenium Automation


Currently we are automating our browsers by giving driver path. For example to open chrome browser, we will give chromedriver path as below:

System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();

Same with other browser also.

Now, with help of below maven dependency we can open browsers without giving browsers driver path:

Add Below Maven Dependency to your pom.xml file:
             <dependency>
                    <groupId>io.github.bonigarcia</groupId>
                    <artifactId>webdrivermanager</artifactId>
                    <version>3.6.2</version>
             </dependency>

How to open edge browser without giving edgedriver.exe file:

package practise;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class SeleniumWithoutDriver {
      
       private WebDriver driver;

       @BeforeClass
       public static void setupClass() {
             WebDriverManager.edgedriver().setup();
       }

       @BeforeMethod
       public void setupTest() {
             driver = new EdgeDriver();
       }
      

       @Test
       public void test() {
             driver.get("https://mahantesh-hadimani.blogspot.com");
             System.out.println("Edge browser opened");
       }
      
       @AfterClass
       public void teardown() {
             if (driver != null) {
                    driver.quit();
             }
       }

}



Execution output:




Console output:




TestNg output:







How to open chrome browser without giving chromedriver exe file:

Use the below code to open the chrome browser:


package practise;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class SeleniumWithoutDriver {
      
      
       private WebDriver driver;

       @BeforeClass
       public static void setupClass() {
             WebDriverManager.chromedriver().setup();
       }

       @BeforeMethod
       public void setupTest() {
             driver = new ChromeDriver();
       }
      

       @Test
       public void test() throws InterruptedException {
             driver.get("https://mahantesh-hadimani.blogspot.com");
             System.out.println("Chrome browser opened");
             Thread.sleep(5000);
       }
      
       @AfterClass
       public void teardown() {
             if (driver != null) {
                    driver.quit();
             }
       }

}


Execution output:


Console output:


TestNg output:



we can use above code for other browsers as well by updating two lines of code :

WebDriverManager.chromedriver().setup();

change the above line without respect your browser.

and initialize the your browser:

driver = new ChromeDriver();


Happy Automation. Cheers !



No comments:

Post a Comment

If any suggestions or issue, please provide