I know there are the Selenium IDE and the Selenium Builder as a Firefox plugin to record actions and export them e.g. as C#-Code.
But is there also a tool which helps you with creating Selenium (2) WebDriver Test-Code following the PageObject pattern? I know that this would be difficult, because the program would have to know how the abstractions should be implemented.
Such a tool doesn't exist as far as I know? But what would be the best way to create Selenium WebDriver PageObject-Code, supported by a tool?
Maybe using the Selenium builder just to create references by id, class name etc. and manually implementing them in my code? This could be a bit faster than deciding on my own what would identify an element precisely. But why did the developers of Selenium Builder create the element references dynamically
IWebDriver wd = new RemoteWebDriver(DesiredCapabilities.Firefox());
wd.FindElement(By.Id("NameField")).Click();
wd.FindElement(By.Id("NameField")).Clear();
and not with the FindBy attribute ?
[FindsBy(Using = "NameField")]
public IWebElement nameField;
The elements would be reusable and the code would be shorter.
In answer to your question, there is no tool and I'm kind of glad there isn't. People rely on these types of tools too much. I would also argue that using the Builder or IDE to do the job for you is not going to be faster. In the short term, yes, long term, definitely not.
Although the builder is a lot more intelligent in it's 'finding', it is still not brilliant. XPath is where it, along with the IDE and even Firebug and Chrome's own developer console, fall over.
What will serve you will, is doing it manually.
However, if you are hell bent on using a tool, then use the Builder (just because it is a 'bit' better than the IDE) to base your code.
In terms of "why" the Builder comes up with that kind of code, it's because it's basic and simple. These tools are meant to give you basic code that works. Not fancy code that is best practice. This is something you should be doing manually. You may find what the IDE comes up with is very similar.
Any one who tries to sell off these tools as the answer to automated testing, is plain wrong. The answer is giving your testers & developers enough time to co-ordinate a decent strategy!
There is no tool that can accurately define a location strategy for a dynamic element. Something that has an ID that changes on each page hit. So what do you expect it to do? It will throw it's hands up in the air and give you it's best shot. This is where you jump in and save the day and make up a location strategy that works - this is where the IDE/Builder is going to save you short term time but you'll spend long-term time fixing it when it falls over.
There is such tool, and I am currently working on this open source project:
SWD Page Recorder
Page Recorder does actually the job this question is about. It allows to record WebElements inside the browser; debug/test locators inside the application form and generate the code of the PageObject class.
SWD Recorder is designed to eliminate the most boring manual operations in the test automation process: the activity of scraping the locators form web-browser and manually converting them into FindsBy attributes.
Page Recorder does not record steps (actions) inside PageObject, and I don't know if it will. I am agree with Arran about that the actions should be written by hand (and using a human intellect).
You can find a short description and video demo following the link above. Also, there is a great blog post from Unmesh, the author of several popular books on Selenium:
PageObject Generator Utility for Selenium WebDriver
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With