My approach to webderiver
I started using webdriver not long ago. My approach is as follows:
public class PageObjectRepresentationClass {
protected WebDriver driver;
public PageObjectRepresentationClass(WebDriver driver){
this.driver = driver;
}
public void open(String url){
driver.get(url);
}
public void close(){
driver.quit();
}
public void fillInputFieldByXPath(String xpath, String value){
WebElement inputField = driver.findElement(By.xpath(xpath));
inputField.clear();
inputField.sendKeys(value);
}
public PageObjectRepresentationClass clickButtonByClassXPath(String xpath){
driver.findElement(By.xpath(xpath)).click();
return new PageObjectRepresentationClass(driver);
}
...
// Basically I make here every possible method that deals with my pages
}
Now, on my Junit test I have:
public class CreateCompanyGermany {
@Before
public void pagefactory() {
page = PageFactory.initElements(new InternetExplorerDriver(), PageObjectRepresentationClass.class);
page.open(url);
}
@After
public void closeBrowser(){
开发者_运维技巧page.close();
}
@Test
public void internetApplying(){
page.open(url );
page.chooseOptionFromDropDownMenuById("String", "String");
page.fillInputFieldByName("String", "String");
page.fillInputFieldByName("String", "String");
page.chooseOptionFromDropDownMenuById("String", "String");
// So from here on I'm just calling methods defined in PageObjectRepresentationClass
}
That is my approach of using webdriver. Now what I would like to know is where should benefit take place in comparing with Selenium 1? I mean if my approach is correct, then only what differs Selenium1 from selenium2/webdriver is fact that in webdriver the one can make sole methods for dealing with pages, so instead of writing
selenium.someMethod(); // derives from selenium API
now I will have
page.myMethod(); // in this particular case derives from PageObjectRepresentationClass
As far as maintaining of code concerns, I do not see any benefit or am I doing it wrong? Thanks in advance!
Selenium 2 won't offer much improvements for the maintainability of testscripts although there are some classes in the support package which are now available which you can use to implement for instance PageObjects.
Have a look at the PageFactory class for instance.
The big difference with selenium 2 is the way it communicates with browsers. Selenium 1 communicates using a javascript server whereas selenium2 webdriver communicates directly with browsers using the browserapi. This has several advantages.
- It will make tests run a bit faster since they dont have to go through a javascript server
- You are not bound to the (security)restrictions of javascript anymore.
- You don't need (but still can use) a server for executing your scripts.
精彩评论