Testing With Selenium WebDriver (68 Blogs) Become a Certified Professional
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (58 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is setProperty in Selenium and how can it be used?

Published on Aug 20,2019 798 Views
5 / 16 Blog from Selenium IDE Fundamentals

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

The primary basis for website testing is to instantiate the browser object and set the system properties of the browser drivers.  That is achieved through the setProperty() method. In this article, I will tell you how does setProperty in Selenium works.

Below topics are covered in this article:

Let’s get started!

What is setProperty in Selenium?

setProperty, as the name says has two attributes which are – “System.setProperty(“propertyName”, “value”)”. It implies that it sets the system property ‘propertyName' to have the value 'value'.

While testing with Selenium, you will make use of the setProperty method because the browser doesn’t have a built-in server to run the automation code. In this case, you will need a Chrome/IE/Gecko driver server for communicating your Selenium code to the browser.

In simple words, to set the path of the driver for the respective browser you will need the system.setProperty.

Now let’s take a small example to understand how it works.

Demo: Illustrating setProperty in Selenium

Take a look at the below code to know its working.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo {
public static void main(String[] args) {
// Here I am setting the system properties of chrome driver and specifying the path to it.
System.setProperty("webdriver.chrome.driver", "C:Selenium-java-edurekachromedriver_win32chromedriver.exe");
// Creating a object to instantiate the browser driver
WebDriver driver = new ChromeDriver();
//Navigating through a particular website
driver.get("https://www.ebay.com/");
//Locating elements using XPath locator for search box
driver.findElement(By.xpath("//input[@id='gh-ac']")).sendKeys("Guitar");
WebElement searchIcon = driver.findElement(By.xpath("//input[@id='gh-btn']"));//xpath for search button
searchIcon.click();
}
}

When you execute the above code, it will launch ebay website in Google Chrome using Chrome Driver where the initialization of the driver is handled by system.setproperty method. This has to be the first line of code that needs to be executed in your selenium script before any test method to initialize the driver. That’s how it works. If you wish to use Firefox Browser and Gecko Driver you can use them accordingly. Learn the working of Gecko Driver with the help of Gecko Driver in Selenium article.

I hope this gives you a clear understanding of how setProperty in Selenium works. Thus, it brings us to the end of this article. 

If you wish to learn Selenium and build a career in the testing domain, then check out our interactive, live-online Selenium Certification Training here, that comes with 24*7 support to guide you throughout your learning period.

Got a question for us? Please mention it in the comments section of setProperty in Selenium blog and we will get back to you.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.