System setproperty in selenium

What is System.setProperty?

If you have noticed in selenium test case, You will find syntax System.setProperty. System is class name and setProperty(String key, String value) is method of System class in java. setProperty method accepts two parameters key and its value.

selenium System.setProperty

Why to use System.setProperty in selenium test case?

For running your selenium test, You need browser i.e. chrome, firefox, edge, safari etc. Selenium can not launch your installed browser directly but it needs specific browser driver to communicate between your installed browser and selenium client libraries. Each popular browser have separate driver in selenium. You need to download that browser driver in which you want to run test. System.setProperty(String key, String value) will decide which browser to launch and provides that browser's driver file path to selenium client libraries.

Let us see with chrome browser example.

You need to write below given syntax to automate test case in chrome browser.

  • System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
  • webdriver.chrome.driver -> used to decide test case will be executed in chrome browser.
  • D:\\chromedriver.exe -> chrome driver executable file path.
So this full sentence will decide in which browser test case will be executed and where that browser's driver executable file is located.

Syntax to launch selenium test in different browsers

You can launch selenium test in different browsers using below given syntax.

To launch test in chrome browser.
System.setProperty("webdriver.chrome.driver", "Path_to_chromedriver.exe");
WebDriver driver=new ChromeDriver();

To launch test in firefox browser.
System.setProperty("webdriver.gecko.driver", "Path_to_geckodriver.exe");
WebDriver driver = new FirefoxDriver();

To launch test in edge browser.
System.setProperty("webdriver.edge.driver", "Path_to_EdgeDriver.exe");
WebDriver driver = new EdgeDriver();

To launch test in IE browser.
System.setProperty("webdriver.ie.driver", "Path_to_IEDriverServer.exe ");
WebDriver driver = new InternetExplorerDriver();

No comments:

Post a Comment