Selenium Course
- 62k Enrolled Learners
- Weekend/Weekday
- Live Class
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.
Let’s get started!
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.
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, which comes with 24*7 support to guide you throughout your learning period.
Find out our Selenium Training in Top Cities/Countries
India | Other Cities/Countries |
Bangalore | US |
Hyderabad | UK |
Pune | Canada |
Chennai | Australia |
Mumbai | Singapore |
Kolkata | Edinburgh |
Got a question for us? Please mention it in the comments section of setProperty in Selenium blog and we will get back to you.
Course Name | Date | Details |
---|---|---|
Selenium Course | Class Starts on 16th December,2024 16th December MON-FRI (Weekday Batch) | View Details |
Selenium Course | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
edureka.co