Starting With Selenium
First, you need to analyze the application you want to automate. Next, you should know whether you want to do it with record and play or writing robust, browser-based regression automation suites and tests.
If you don’t need a full-fledged framework and if the tests which you want to automate are quite simple then you can go for record and play feature of Selenium IDE. It is implemented as Chrome and Firefox Extension. It has intelligent field selection which can identify an element using ID, Class and Xpath.
- Creating WebDriver Instance: You need to create Webdriver instance. You can make a reference of WebDriver and you can have child object of its implementation classes like GoogleChrome, Internet Explorer or others.
WebDriver driver= new FirefoxDriver();
WebDriver driver = new ChromeDriver();
WebDriver driver = new InternetExplorerDriver();
Also, keep in mind that if you are using ChromeDriver and InternetExplorerDriver then you need to set path for their executable file with the help of System.setproperty.
You don’t have to set it for firefix but if your selenium version is above 3.0 then you need to set path of gecko driver in your code.
- Navigate To Webpage Which You Want To Test: After making instance of webdriver now it is time to actually open the web application which you want to test. You can navigate to webdriver using get or http://navigate.to.URL() function.
driver. get(“URL”); // This will land you to our webpage which you want to test.
- Locate an HTML Element on Webpage: After you have landed on the webpage you can now interact with the webpage using HTML elements. There are many locators which you can use to find out the elements. They are name, class name, Xpath, CSS and ID. You can take help of firebug or developer tools to find out your desired element.
WebElement login = driver.findElement(Domain Premium: By.id(“”)); // one example of locator
- Perform an Action on WebElement : When you have found web element, now comes turn to do some action on it. For example if it is username you can enter words from keyboard and if it is login button, you simply have to click on that.
username.sendKeys(“”); // for sending input from keyboard to username textbox
login.click(); // for clicking on login button
- Anticipate The Browser Response to the Action: Browser takes some time to actually load the page. You should have some default time wait for all the web elements till the life time of driver instance. This can be done by implicit wait in Selenium.
driver.manage().timeouts().implicitly Wait(10,TimeUnit.SECONDS) ;
This time would be applicable for all web elements. But sometimes there is a situation when only one element takes longer time to load then in such cases you can use explicit wait.
WebDriverWait wait=new WebDriverWait(driver, 20);
You can have N number of conditions with wait object.
- Close Browser Session You can close the browser the required test is performed.
driver.close(); // will close the current session
- Run Tests and Record Test Results Using a Test Framework Whatever I have explained till now will help you to run a single test. You can even maintain a framework to run test suites with beautiful report generation. You can take help of Maven, POM and TestNG to make such a robust framework.