Selenium is one of the most popular open source testing automation tool. The best thing about any tool can a big community support, Selenium has very vast community and it has multiple tools to ease your work. In this blog, I am going to discuss about ‘Setup And Write Selenium Scripts’ . Here is the agenda.
- Why should you use Selenium?
- What are tools involved in Selenium and which tool is right for you?
- How to use Selenium IDE to generate the selenium scripts?
- Finally, writing code to cover test cases (Not really required if you selenium IDE).
Why should you use selenium?
Tools involved in Selenium
There are multiple tools provided by selenium to create and run the tests.
Selenium Webdriver is the interface which accepts commands from Selenium clients and executes on the respective browser. In simple term, we can selenium web driver opens up a browser and run the commands on that browser. Every browser will have there own web drivers. Here is the link you can use to download the web drivers.
Selenium Clients (Web drivers client).
Selenium clients are nothing but the interface which is used by our code or program to deal with selenium servers. Let take an example of simple HTTP service, any site which is available on browser acts as server and browser acts as a client. Same way even our codes requires a selenium client to deal with selenium server.
In order to write selenium script, you will need a selenium client as well. There is list of clients available in selenium sites, depending on what coding language you use, you can pick respective clients.
Selenium IDE is a tool provided by selenium to records the actions which you can use to create selenium script. This is really a timesaver for you. Rather going through the complete documentation on selenium before starting on writing selenium code, you can directly go in your browser and start recording your action and selenium IDE will give you the option to export your recording as test scripts.
Use Selenium IDE to generate the selenium scripts.
Steps to setup and record selenium IDE.
Selenium IDE is only supported for Firefox, this does not mean you can not run your script from another browser. Script generation needs Firefox, but the script can be run for any browser.
Firefox and Selenium versions
Due to going demand of browsing speed and being robust against security threats, the browser has kept itself updated and with this even Selenium has to be updated. To get rid of this problem you can plan to have a fixed version of the browser and fixed version of selenium. For example, I use selenium and firefox inside a VM and have disabled all the browser updates. This keeps my test cases running. I am using Firefox version 54 and selenium IDE 2.9.1
To install firefox go below links and install.
To install Selenium IDE go to firefox addons URL.
https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/ In case you do not find any supported version, check the plugins supported version and download that specific firefox version. https://ftp.mozilla.org/pub/firefox/releases/
Go to tools (press ALT) in Firefox and click on Selenium IDE.
Click on the record button.
Stop recording once you have manually run the test.
Now go back to Firefox and start running the test steps manually. Once it is done click on that red button again in Selenium IDE to stop recording the script. Now you will there is lot lines generated in Selenium IDE. These are the commands which can be run again to repeat the steps. These commands can be exported as a test script in any language you want. One thing to note here you sometimes these scripts are not accurate and you have to manually edit scripts.
Export the test script to your favorite language.
Go to File -> Export Test Cases -> Select web driver with your preferred language. It will create respective script files. This file you can directly use to run the selenium test case.
Writing code to cover your test cases.
The last step to this is, Once the code is exported you can import it in your favorite IDE (eclipse, Netbeans, Visual studio code etc) or you can directly run it using command line commands supported by the language. For example java. java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
Here is an example how it looks in Eclipse. Also, notice I have geckodriver.exe as well in my libraries, which is nothing but a firefox Webdriver. You can download it from selenium site for another browser as well.
Once you finished creating all your test scenarios you can configure it to run automatically either by scheduled cron jobs or use Jenkins for continuous integration. Jenkins is an open-source continuous integration tool, You can automate a lot of dev ops stuff like build, deployments, testing, monitoring etc. For this please read my another blog on configuring selenium with Jenkins.