QA/Execution/Web Testing/Selenium Guidelines

From MozillaWiki
< QA‎ | Execution‎ | Web Testing
Revision as of 02:43, 13 February 2010 by Stephend (talk | contribs)
Jump to navigation Jump to search

Here be some Selenium guidelines (mostly for writing, but include running here too).

Setup information

See Raymond's documentation, here: https://intranet.mozilla.org/User:Retornam@mozilla.com/Selenium/#qa-selenium.mv.mozilla.com

Pre-checkin requirements

  1. Testcase/suite naming structure:
    1. Give meaningful names to testcases
      1. Testcase: e.g.: collections_addNewCollection.py (testarea_nameOfTestCase.py)
      2. Suite: suite_Collections.py ("suite" is required and standard; _Collections.py is whatever area you're writing it for)
      3. Don't use hyphens, as those are illegal in Python class names
  2. Have the testcase reviewed by a peer, following these requirements
  3. Comment briefly about the flow and aim of the testcase, as well as commenting inline
  4. Store XPath variables at the top of the testcase, naming them in a unique and identifiable way
  5. If you're using a regular expression, comment before using it for what you're looking
  6. Add-on IDs are different between production and preview, so if your testcase uses a specific add-on ID, make sure to note that at the top of the testcase in the comment section

Testcase template / structure

from selenium import selenium
from GridConnection import GridConnection
import unittest

class testamoAdmin(unittest.TestCase):
    def setUp(self):
        # import the connection to the server information from the grid connection config file
        connection= GridConnection()
        self.selenium = selenium(connection.server,connection.port, \
            connection.macFirefox, connection.amoStaging )
        self.selenium.start()
    
    def test_Login(self):
    code...

    def tearDown(self):
        self.selenium.stop()
        
if __name__ =="__main__":
    unittest.main()

Some good practices

  • It's best to check if you are logged in at the beginning of every test case. At the end of the test case, always logout.

Example: StoreElementpresent link=log in not_logged_in

gotoIf ${not_logged_in}==true target

clickAndWait link=Log out

label target . .

Run scripts on all environments

  • If you want to run your scripts on all environments, then code your setUp() function like this:
      def setUp(self):
      self.verificationErrors = []
      self.objConnection = GridConnection.GridConnection()
      for envCounter in self.objConnection.remoteControls:
          self.selenium = selenium(self.objConnection.server,self.objConnection.port,envCounter,self.objConnection.amoStaging)
          #self.selenium = selenium('localhost',4444,"*firefox","http://preview.addons.mozilla.org")
          self.selenium.start()

IDE

  • click vs. clickAndWait - use the latter on anything that requires network activity, but if you use the former on other buttons/links, your script will pause indefinitely
  • verifyLocation - use w/regexpi; otherwise it'll be absolute (I think)
    • e.g. ...
  • wherever possible, use relative--not absolute--URLs in |open| commands
  • Store xpath you use in the testcase in variables at the beginning of the testcase and use the variable whenever you need to use the xpath.This way,if you ever need to change the xpath,you change it only once at the top of the testcase.
  • storeLocation/verifyLocation usage:
    • after a storeLocation, verifyLocation by:
      • verifyLocation regexpi:.*/en-US/firefox/admin.* (e.g.)
  • Add an echo statement after every assertion.
  • StoreXpathCount command can be used to find the number of elements having a similar xpath.
  • VerifySelectedLabel can be used to verify the selected option in a dropdown box.

PHP

  • Locating elements
    • xpath: for elements that have dynamically generated xpaths use 'contains'.
  for e.g. if xpath for 'remove add-on' link has the add-on ID 
  in it (id('addon-2464')/a[1]) and we do not know the add-on ID then
  we can write the xpath with 'contains'
  //div[@id,'addon-2464'][2]/a[1]) => //div[contains(@id,'addon- ')][2]/a[1]
    • If you want to match on the text in an element
  //a[text()=’My Text’]: matches a link whose text is “My Text” 
    • If you want to match more than one string in attribute or text
  //a[contains(text(),’Bill’) && contains(text(), ‘Clinton’)]:matches a link
  whose text contains both Bill and Clinton. You can use any xpath function or
  operator to create your expression   
    • Find the nth element of a type
  //div[position()=3]: matches the third div on the page
  //div[@id=’abc’]//table[position()=3]: matches the third table inside the div
  whose id is abc