Wednesday, 20 March 2013

How to store & assert HTML-meta-tag?

<META> tag is a special HTML tag that provides information about a Web page. Metadata will not be displayed on the page, but will be machine parsable.

Unlike normal HTML tags, meta tags do not affect how the page is displayed. Instead, they provide information such as,
  • who created the page(author), 
  • how often it is updated, 
  • what the page is about(page description), and 
  • which keywords represent the page's content. 

Many search engines use this information when building their indices. This is how it looks:

<meta name="description" content="Free Web tutorials">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="Ståle Refsnes">
<meta charset="UTF-8">

Note: <meta> tag always goes inside the <head> element.

Store & Assert <meta> tag 'Description' content:

1. Use the command, "storeAttribute" for storing contents present inside the <meta> tag.
2. Insert the xpath //meta[@name='description']@content in Target field.
3. Now store the value and print it.

storeAttribute  |  //meta[@name='description']@content  |  variable
echo  | ${variable}

Java code for store and assert <meta> tag description contents:

String variable= driver.findElement(By.xpath("//meta[@name='description']")).getAttribute("content");
assertEquals("your text", variable);

Monday, 18 March 2013

Headless Browser Testing using PhantomJS - GhostDriver | WebDriver

PhantomJS is a Headless Webkit with JavaScript API. It has fast & native support for various Web Standards: DOM handling, CSS selector, JSON, canvas and SVG.  GhostDriver is a Webdriver wire protocol in simple JS for PhantomJS.  PhantomJS is used for Headless Testing of Web Applications that comes with in-built GhostDriver.

1| General command-line based testing.
2| As a part of a Continuous Integration System.

PhantomJS is not a Test framework, it is used only to LAUNCH the tests via a suitable Test Runner.

Framework used: WebDriver
Test Runner: GhostDriver

CI systems: Make sure PhantomJS is installed properly on the slave/build agent and it is ready to go. Headless Browser is a Web Browser without a GUI (Graphical User Interface). It access Web Pages but doesn't show them to any human being. Headless Browser should be able to parse JavaScript.

Configure PhantomJS

1. Download phantomjs.exe
2. Extract the folder and locate phantomjs.exe file to C:/ folder
3. Add the following imports to your code:

import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

4. Replace the object, "driver" specifying "FirefoxDriver" with "PhantomJSDriver".

Replace the code,
WebDriver driver = new FirefoxDriver

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // not really needed: JS enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);

5. Run Test.

PhantomJSDriver-1.0.x.jar can also be downloaded and configured in Eclipse manually.

PhantomJS | Screen Capture

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // not really needed: JS enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C://phantomjs.exe");
caps.setCapability("takesScreenshot", true);
driver = new PhantomJSDriver(caps);  
baseUrl = "";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

public void test01() throws Exception {
driver.get(baseUrl + "/");   
long iStart = System.currentTimeMillis(); // start timing
<your script>
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\sample.jpeg"),true);    
System.out.println("Single Page Time:" + (System.currentTimeMillis() - iStart)); // end timing    

Friday, 8 March 2013

Load Default/Custom Chrome Profile to run tests using Selenium WebDriver

1. Download Chromedriver 2.4
2. Extract the zipped folder and locate .exe file to C:/ folder

ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
driver = new ChromeDriver(options);

If you face such error:
"org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited normally"

Then try to create a new Chrome profile and execute tests.

1| Copy the folder, 'User Data' and paste it on the same folder with different name. e.g., New User
2| Open the folder, 'New User'.
3| Rename the folder, 'Default';  so that after your test run, a new folder named, 'Default' will be created.
4| Now, replace the directory on your code with C:/Users/user_name/AppData/Local/Google/Chrome/New User
5| If you like to test the profile, then bookmark some of the sites & observe them on next run.

IE doesn't need profile setup to run tests because they run on Server user while Firefox and Chrome works with binary.