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".

Saturday, June 12, 2010

String match using Regular expression in both Java and Ruby

sometimes, I will looking for some dynamic data/elements from HTMLsource to make my automation scripts more robust and reliable, so using regular expression to do string match is very useful for me to get them:

Example in Java( for selenium usage):

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexTestHarness {

public static void main(String[] args){


Pattern pattern =
Pattern.compile("information-abc-([0-9]+)");

Matcher matcher =
pattern.matcher("information-abc-9887234-information-abc-223333889-information-abc-12233");

while (matcher.find()) {
for (int i=1; i<=matcher.groupCount(); i++) {
String groupStr = matcher.group(i);
System.out.println(groupStr);
}
}

}
}


Example in Ruby( for Watir usage):

def regex_match(search_pattern)
source_string = "information-abc-9887234-information-abc-223333889-information-abc-12233";

source_string.scan(/#{search_pattern}/) { |match| puts match;}

end

regex_match("information-abc-([0-9]+)");


Love Ruby, it is simple :)

Friday, June 11, 2010

World CUP 2010 , Right here, Right now!

Two talented team i will support:

- Argentina
- Mexico

Let's enjoy the Game from Today!:)

Wednesday, June 09, 2010

SoapUI MockOperation Dispatching in Groovy script

SoapUI provides several ways to dispatch mock service response, you can find the detail information from: http://www.soapui.org/Service-Mocking/simulating-complex-behaviour.html.

If you want to manage your mock response dispatch more flexible, then the Script way is your choice.

Here is the sample script you can select your mock response according to the different requests:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def holder = groovyUtils.getXmlHolder(mockRequest.getRequestContent());

//Get node value from Xpath
def OID1 = holder.getNodeValue("//v2:getRequest[1]/v2:OIDs[1]");
def OID2 = holder.getNodeValue("//v2:getRequest[1]/v2:OIDs[2]");

//if condition 1 is matched, return mockresopnse1
if (OID1 == "C1308C7C-097F"&& OID2 == "") {

mockOperation.setDefaultResponse("mockresopnse1");

}
//else if condition 2 is matched, return mockresopnse2
else if (OID1 == "A097B4E5-0159" && OID2 == "17001212-AABF") {

mockOperation.setDefaultResponse("mockresopnse2");

}


PS: Due to lack of documentation and sample scripts on this, I spent me several hours to do some investigation on it, the only thing to do is "reading the APIs and have a try".
Hope it helps,And Thanks for Lynn and Vance's help on this as well! :)

Tuesday, May 25, 2010

Bad response on Firefox with enabling Gzip

While functional testing with enabling gzip(We are using Apache/2.0.49) bring me a bad news, there is randomly returns bad response instead of 200 or 304 response code with Firefox/3.6.3:



Apache access.log has indicated the .css file return 304 Not modified, however, FF seems neither receive 304 response code successfully nor retrieve it from local cache. So the page looks wired.

Another issue is that the page will display the irrecognizable characters in its response, something like this:
���������HTTP/1.1 200 OK Date: Tue, 25 May 2010 07:30:02 GMT Server: Apache/2.0.49 (Win32) DAV/2 mod_ssl/2.0.55 OpenSSL/0.9.8e mod_jk/1.2.15 X-Powered-By: Servlet 2.4; JBoss-4.0.4.GA (build: CVSTag=JBoss_4_0_4_GA date=200605151000)/Tomcat-5.5 Vary: Accept-Encoding Content-Encoding: gzip Keep-Alive: timeout=15, max=88 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: text/html;charset=UTF-8 a ¬� ������2d3 �VQo...


Meanwhile, IE seems works fine. Does it mean its a Firefox issue? Or it is a Apache 2.0.* gzip issue? (As i did not see such issues reported by Apache 2.2 version)

After taking simple search, there are other people met the same problem as well:
http://forums.mozillazine.org/viewtopic.php?f=38&t=1121915&p=9413795#p9413795

I tried Apache2.2.10, but it seems preventing 304 Not modified response, which is a bug of apache:
https://issues.apache.org/bugzilla/show_bug.cgi?id=45023

Anyone met the similar issues? Any better solutions?

Friday, May 21, 2010

"Firewall(?)" blocks "Accept-Encoding" header

recently, I did Gzip testing with loadrunner with "Accept-Encoding: gzip, deflate" request headers, so want to make sure that Gzip rocks as i told to my teammates.

My test scenario is running from remote server which located in US DC, it sent requests to China servers.

However, i noticed the test result is not changed, then i doubt about Gzip impact, keep look at apache access logs by adding \"%{Accept-Encoding}i\" to LogFormat in httpd.conf. There is no "gzip, deflate" header in access log found:
"GET / HTTP/1.1" 200 2788 0 "-" "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)" "-"


Then I did another round of test from local desktop, then everything is fine:
"GET / HTTP/1.1" 200 2788 0 "-" "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)" "gzip, deflate"


So "Firewall" blocks "Accept-Encoding" header is just my assumption, but not quite sure... At least notice your test result is making sense, otherwise, you are wasting of time, do not let such test result misleading!

Update:
Find a person with similar issue as mine: http://forums.mozillazine.org/viewtopic.php?f=9&t=1483185
There is Outpost Firewall which disable gzip...

Another latest post on "pushing beyond Gzipping" by Yahoo! talking about forcing Gzip when Accept-Encoding request header is mangled or stripped.