Friday, April 8, 2016

Selenium, finally some progress

After getting the hang of Java and being able to produce some workable code, I wanted to go ahead and automate some tests. After all, 6 weeks had passed in my new career adventure and not a single testcase had been automated. It was about time!
With some help of smart googling, I ran into this blog and training series helping me use Selenium, JUnit and IntelliJ, the all in one package I was looking for. Really useful as it will teach you how to use Maven within IntelliJ, what the JUnit framework can do for you and after this setup how to build some basic Selenium Scripts. He changed the layout since I used it and moving from one post to the next became weird, so let me build it out for you here.

Newbie friendly guilde to using Selenium in IntelliJ.
Part 1: Install Maven and do some basic setup.
Part 2: Install IntelliJ
Part 3: Get Testing

These are the basics and I don't want to steal this guys pageviews so go here for a full overview. Posts I can recommend are:
Firebug and Firepath, very useful with Selenium. These will allow you to find out what that element you are trying to locate actually is called and how to refer to it.
Using Assertions. It is nice you can click a button but is the result what you expect it to be?
His series on waits is useful knowledge as well. In fact, I would recommend you check out what classes and methods (you remember this from Java, right?) are being offered in the greater Selenium package. To do some straightforward reading on this you go here, it took me some searching as for some reason it is not that clear and open in the Seleniumhq.org documentation. Things are a bit scattered as technical documentation has a tendency to do but realize the following:

With Selenium Webdriver you can do whatever a user can do in a browser screen in terms of clicks, movements, scrolling, typing and whatever you can think of. Even better, this can be done in multiple browsers, across platform and with a wide variety of recordings such as screenshots, textfiles and reports.

The commands allow you to do all that, you just will have to figure out how to tell Selenium to do exactly what you want in the browser. This sounds like quite some effort to get the hang of and it is so I am using as many things I can find to make my life easier.

The one I like best is using the Selenium IDE to record and then export to Webdriver/Java/JUnit. Some people, maybe even most people, would consider this cheating but allow me to explain.

The Firefox addon SeleniumIDE which we installed a few posts back allows me to record in any webpage what I click, where I type and so on. There is a certain order to things and it can be found in the Options screen under the tab Location Builders, like this:





This lists what it will pick first, what second and so on. Basically, if it cannot find a locator of a certain type for the object you click, it will move one down and in the end the Xpath remains which will always work but is a real pain to maintain.

So what I do is the following.
Open website and open Selenium IDE addon.
Click record on the Selenium IDE window. Do not forget this, else you have to redo.
Record whatever you are doing.

Your window will now look like this:


There are some actions that I now want to extract from this to use in my Java/Selenium Webdriver/JUnit test scripting and here is how to do that:



Yes, we can actually export that to the exact setup we are using, so let's go and do just that, put it in a nice place where you can find it such as the desktop and do not treat it as anything as a super temporary file.

Now open Notepad++ and navigate to the super-temporary-file.java on your desktop. There will be a ton of code that you really don't care about in the grand scheme of your laziness so we ignore that and look up the selection bit, highlighted here:


Look at that, you copy/paste this to your java code in IntelliJ or Eclipse or whatever you are using and edit it how you want it to behave but it saves so much time in trying to figure out the most optimal way to pick an element. Also screw that the picture is to wide, we need all that booty to show a point!

Hoping I angered many purists and made a lot of lazy people happy, I will move to Git next time as Git is a good thing to have, know and use.

















1 comment:

  1. Hi Anna, I get the impression the first is more about automation and the second more about general qa training. Best you can do is reach out to the training provider and ask them what the best training is depending on your current skills and goals.

    ReplyDelete