Thursday, July 22, 2010

Deal with JS div DialogPane in WebDriver

Updated: Another related post on js alert and confirmation stuff on http://joychester.blogspot.com/2010/08/deal-with-javascript-alert-and-confrim.html

During my testing, I met one "Send" link which will trigger js DialogPane to get your confirmation, such "OK" or "Cancel", onclick will execute JavaScript function like this for "OK" :

onclick="confirmSend()"

Here is a sample code how I simulate clicking "OK" within the js DialogPane:

driver.switchTo().activeElement();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("confirmSend()");


Thanks for Shawn's Demo code, she gives me really great suggestion and inspiration!! :)

“Raft” Logo

Friday, July 16, 2010

Take screen shot on WebDriver

Simple code on take screen shot on WebDriver(InternetExplorerDriver,FirefoxDriver and ChromeDriver):

FirefoxDriver driver = new FirefoxDriver();
....
File pngFile = new File("D:\\test\\", "shot.png");
File tmpFile = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(tmpFile, pngFile);

Thursday, July 15, 2010

Get WebElement within PageFactory class by refelction

I just wandering how to get PageFactory's private WebElements in another test method?
Here is the code which Vance Zhao provided a way to find WebElements by reflection:


......
FSILoginPage landingpage =
PageFactory.initElements(driver, LoginPage.class);

Field f = landingpage.getClass().getDeclaredField("username");

WebElement e = new DefaultElementLocator(driver, f).findElement();

e.sendKeys("123456");
.....


Here is the PageObjects code:

public class LoginPage {

private WebElement username;
......


I will not consider to do this kind of coding in our test code , it is really a “Demo code”.

I will try to Abstract public methods in PageObjects Class which is a really worth stuff to do and make testing more fun and challenging! And also consider make WebElement to public as a workaround :)

The fast way to locate page elements on PageFactory

From PageFactory.java source code, it has a demo that without @FindBy annotation, it will find page element using Xpath by default, which is a Clean and Smart way, however not a fast way:

public class Page {
private WebElement submit;
}

there will be an element that can be located using the xpath expression
"//*[@id='submit']" or "//*[@name='submit']"


So from performance perspective, I will be glad to use @FindBy annotation way:

public class Page {
@FindBy(How.ID_OR_NAME, using="submit")
private WebElement loginbutton;
}

Tuesday, July 13, 2010

Emulating selenium APIs within WebDriver

Emulating selenium APIs within WebDriver which allows for the WebDriver and Selenium APIs to live side-by-side, a simple sample FYI:


WebDriver driver = new FirefoxDriver();

driver.get("http://www.google.com.hk/");

//PageObjects design pattern
GoogleSearchPage googlesearchpage =
PageFactory.initElements(driver, GoogleSearchPage.class);

String baseUrl = driver.getCurrentUrl();

//Change into Selenium instance
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

//Using selenium method
selenium.type("q", "Gmail");
selenium.click("btnG");

//using PageObjects and driver
googlesearchpage.clearsearchtext(driver);

googlesearchpage.searchfor(driver, "joychester");

//change back to Webdriver instance
WebDriver driverInstance =
((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver();

googlesearchpage.clearsearchtext(driver);

googlesearchpage.searchfor(driverInstance, "Gmail");

//close browser and stop selenium
driverInstance.close();
selenium.stop();

Quit completely when emulating Selenium API in WebDriver

Once you Emulating Selenium API in WebDriver code, you need to quit your service like this separately:
driverInstance.close();
selenium.stop();


......
String baseUrl = driver.getCurrentUrl();

Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

selenium.open("http://www.google.com.hk/");


WebDriver driverInstance =
((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver();

googlesearchpage.searchfor(driverInstance, "Gmail");

driverInstance.close();
selenium.stop();
......

"Make it Small, Do it Clean"

Currently, I am going to work on an automation framework design based on Webdirver and TestNG with my intern James, here is one screen shot on project folder structure where QA can organize their tests:



Try to "Make it Small, Do it Clean".