Showing posts with label browser. Show all posts
Showing posts with label browser. Show all posts

Thursday, May 03, 2012

Some selenium tips

UPDATES
  • May 31, 2012: Added the tip to capture the screen when test fails.
  • May 11, 2012: Added the tip that about keeping the browser window displaying (or hidden) during the tests.
  • May 11, 2012: Rephrase the scroll to a button before clicking it tip.

Watch out the outdated articles on the internet.

Selenium 2.0 is completely different from Selenium 1.x. Selenium 2.0 is also called the selenium webdriver. So always add the keyword webdriver when googling for answers to your selenium related questions.

Implement the web UI in a modular way so it's more selenium testable.

Modularize your view logic so that you only update the part of DOM that is needed to change when your models change. If you tend to re-create a bigger part of the DOM than necessary, it's not only a waste but also could introduce risk to your functional tests written in Selenium.

Reduce unnecessary dependency on DOM structure, make element locating logic as simple as possible.

When you need to locate an element, try not rely on the DOM structure too much - for example, using code logic to locate element is the most risky one. The best approach is probably to always use a scoped CSS selector with 1 or 2 levels of IDs, And if you can locate it in one selector, don't do it in two. For example
  label = driver.find_element("#info-panel #name-label")
is more robust than
  panel = driver.find_element("#info-panel")
  label = panel.find_element("#name-label")

Do waiting in selenium the smart way.

Don't use implicit wait blindly. Implicit wait makes sense when you use find_element to find one element. But when you try to locate multiple elements by driver.find_elements, the driver will always wait the whole timeout period if implicit wait is set. That might not be what you always want. I usually write my own safe find_element method. Here is an example in the base class of my page objects:
    def s selector
      wait_until { @driver.find_element css: selector }
    end

    def wait_until(&block)
      wait = Selenium::WebDriver::Wait.new(timeout: 10, interval: INTERVAL)
      wait.until &block
    end
So that I can write the following code in my page object
   def submit_order
     s('button#submit').click
   end
The short method name "s" is inspired by jQuery. Here it will keep polling the DOM for 10 seconds until it finds the button with id "submit". It's like implicit wait but only for finding one element. When you really need to wait for multiple elements, you can use an explicit wait, which, to me, makes more sense than a hidden implicit one.

Set the initial browser window size when using Chromedriver.

Ruby code:
  profile = Selenium::WebDriver::Chrome::Profile.new
  profile['browser.window_placement.top'] = 0
  profile['browser.window_placement.left'] = 0
  profile['browser.window_placement.right'] = 1024
  profile['browser.window_placement.bottom'] = 768
  driver = Selenium::WebDriver.for :chrome, profile: profile
This works in both Windows and OSX (will try Linux and update here)
Bad news for Java, C# and Python coders though, it seems that as of now setting chrome preference is not supported in the java version of Webdrive. Your best chance could be creating a ChromeProfile class based on the exiting FirefoxProfile class.

Scroll to a button before clicking it.

Clicking buttons sometimes randomly fail. It could be caused by the fact that the button has to be the view area to be clickable and somehow the selenium auto scroll failed. In this case, add a scroll to button will improve the robustness of your suite.

When running the test using Firefox, it matters whether the browser window is displayed on the screen or hidden behind other windows.

From my experience, my guess is that selenium interacts with the browser in slightly different ways depending on if the browser is displaying in the front or not. There is rare case that certain selenium operations only work when the Firefox is displaying in the front. When running the selenium suite, changing the z position of the browser window (and thus either show or hide the browser window from other applications) can affect the tests. So you get more consistent results by keeping the browser either showing in the front(or hidden in the back) during the course of full suite.

Capture screenshots when test fails (RSpec)

In your spec_helper.rb
  RSpec.configure do |config|
    config.after(:each) do
      capture_screen_when_fails(example, @page) 
    end

    def capture_screen_when_fails example, page
      if(example.exception.present? and page.present?)
        page.capture_screen(example.description) 
      end
    end
  end
Note here that you need to keep your page object instance in an instance variable(in my case @page) in your spec. I used a naive way to name the screenshot after the example's description.
Now in your base class for your page object.
    def capture_screen filename
      path = "PATH_TO_TMP/#{filename}.png"
      @driver.save_screenshot(path) 
    end
Note that @driver is the instance variable holding the selenium webdriver currently running.

Sunday, March 20, 2011

iPad2 vs Xoom - Internet browsing

This article compares the Internet browsing experience on the two devices (Xoom running Honeycomb 3.0.1 vs iPad2 running iOS4.3), in the following 7 categories:

Operation
The quick control system on Honeycomb browser allows you to go back, forward, refresh, bookmark and other actions using your thumb when holding the tablet. This is not a default setting. User will need to enable it in the settings. Once enabled, the action bar will disappear and thus give you more real web page display. You can popup an action menu by swiping either of your thumbs from edge. The popup menus will display where your thumbnail swipes and is arranged around the thumb so that you can easily click an item.



On iPad2, you move your hand to click on the buttons on the top.

Winner: Xoom




Multitab Browsing
On iPad2, to switch between tabs, you have to click the tabs button then chose a tab from the new tab grid view, this is a bit cumbersome especially if you are familiar with multi-tab browsing on a full pc/mac browser. On honeycomb, tabs are always displayed on the top just like they are on a desktop browser and switching tabs is a 1-click thing.
Also, Xoom has 1GB memory while iPad2 has 512MB, whether this translates to more tabs supported on Xoom is yet subject to test.

Winner: Xoom




Typing
The touch screen keyboard on iPad2 is designed for a phone (just like the whole OS) and thus not suitable for the bigger tablet layout. Typing on it is even slower than typing on iphone, basically you are forced to type a full size keyboard using one finger. On honeycomb, you can download the thumb keyboard input method from market (free). This soft keyboard layout the keys around the bottom left and right corner so that you can type using both thumbs while holding the tablet with both hand, pretty much like when you type on smart phone with both thumbs, and thus achieve the same typing speed.



Winner: Xoom




Screen real estate
Xoom has a higher resolution 1280x800 comparing to ipad2's 1024x768. This means Xoom displays more content than iPad 2 when viewing the same page.

Winner: Xoom



Color Rendering
iPad2 is the clear winner in this category due to the superior IPS screen it's using while Xoom is using TFT which is common on laptops. Basically Xoom gives you the same color as normal laptop, while iPad2 gives you the color as on macs.

Winner: iPad2



Flash
Xoom supports Flash while iPad2 doesn't. It means there a great number of websites that xoom can visit normally while iPad2 simply can't. According to a survey done in 2008 , somewhere between 30% and 40% of all pages tested contained Flash files. Most popular websites already started to provide versions compatible to iPad, but it could be annoying when you bump into one that doesn't.

Winner: Xoom




Speed
According to several tests, when both on the same wifi network, Honeycomb browser on Xoom is on par with if not slightly faster than safari on iPad2. However, when without Wifi availability, Xoom will be able to work on the LTE 4G network (after the coming free upgrade from Motorola) while iPad2 can only work on the slower 3G network.

For now: tied, Winner in the near future: Xoom