Dieses Blog durchsuchen

Mittwoch, 3. August 2016

Create a selenium browsertest with java in eclipse

Now we want to create our first simple automated selenium test with java and eclipse.

For that we need selenium server , java and eclipse running on our machine.

If you do not have the prerequisits installed, you can follow this tutorials.

http://magento2-tuts.blogspot.de/2016/08/install-selenium-rc-server-2-3-on.html

We want to use chrome as testing browser. So you need to have chrome on your machine.


Okay lets start:
 1) create a new javaproject in eclipse
- start eclipse
- select new -> java project
- give it a name. For instance: "selenium test"
- just let the configuration in default and click finish

3) include the selenium server jar to the main library sources.
- Rightclick on your project. Choose Properties->Java Build Path

Here you choose "external library" from the tab and search your selenium server jar.




If you do not have one, you can dowload Selenium Standalone Server here:
http://www.seleniumhq.org/download/

Click apply and the server is added to your external library folder.


Now we can start writing our first testcase

- Rightclick on src and add a new class.

- Set it up like that


Configure a chrome webdriver
At next we have to tell our test which driver we want to use. We want to use chrome as a webrowser. So we have to install the chrome webdriver

Download chrome webdriver.
- http://chromedriver.storage.googleapis.com/index.html?path=2.22/
- move the zip to /usr/bin/selenium
- unzip the package there.
- after that you have an executable file under /usr/bin/selenium/chromedriver

 Write a first testclass

Back to your eclipse ide you find your javaclass "googleSearch". In that class you now import the chromedriver and tell the code to use it.
This is my class:


As you can see, we just import the webdriver class and the chromedriver class.
After that we tell the class to set a systemvariable to the chromedriver and reference an object from type chromedriver to a local variable "driver"


If you now run the program, chrome should start up with an empty page.

Conratulation. You have your selenium server running and you are able to drive now,

Create a more complex test
At next we want to start a test which opens google and searches for a text typed in th searchfield.


The Testtasks are:

- define browser
- get a driver
- open "http://google.com"
- type in "magentotuts"
- click enter
- printout the pagetitle.

This is a very simple test, but it shows, how to automate the pageclicks.


Thats it ! Feel free to have fun with it.



Keine Kommentare:

Kommentar veröffentlichen