Sunday, 30 August 2015

jenkins

1. jenkins is a ci tool
  1. (optional) scm                                //where jenkin find source code to build, git etc
  2. (optional) trigger to build
  3. build script to perform the build    //ant, maven, shell script, batch file etc
  4. (optional) steps to notify                //email, im, issue tracker etc

2. install jenkins on windows
  1. download jenkins windows installer
  2. run installer will install jenkins as a windows wervice, and it runs on local SYSTEM account.  so it will use 'system environment variable' instead of 'user environment account variable', which will cause trouble (JAVA_HOME etc missing)
  3. also it will not allow interaction with desktop (selenium browser will run in the background)
  4. so instead, after run installer, stop jenkins windows service, and run jenkins as 'java -jar jenkins.war'

3. jenkins configuration
  1. jenkins configuration will read  environment variables from windows account
  2. jenkins allow user to change environment variables

4. build non source control code
  1. build empty project (will fail), but jenkins will create folder jenkins/workspace/PROJECTNAME/
  2. copy local project files to the above folder
  3. build again

5. jenkins email notification
  1. if using gmail, need to change 'Sign-in & Security' -> 'Signing into Google' -> 'Allow less secure apps: ON'

6. jenkinsfile (declarative and scripted syntax)
      pipeline {                                                                //declarative
          agent any
          stages {
              stage('Build') {
                  steps {
                      //
                  }
              }
              stage('Test') {
                  steps {
                      //
                  }
              }
          }
      }

      node {                                                                      //scripted
          stage('Build') {
              //
          }
          stage('Test') {
              //
          }
          stage('Deploy') {
              //
          }
      }

7. jenkinsfile
  • sh """ ... """                       //multiline, sh is a 'step' provide by plugin
  • def                                     //groovy, define var or func
  • readProperties                   //read file in java property file format

8. parameterise pipeline
  • use <JENKINS URL>/pipeline-syntax             //snippet generator
  • select 'properties' step to generate code

9. jenkins github integration webhook to jenkins
  • trigger build whenever commit pushed
  • use 'ngrok' if jenkins is localhost
  • on github side, config github-webhook
  • on jenkins side, github-webhook trigger build when commit happens

10. restart jenkins
  • (jenkins_url)/safeRestart
  • (jenkins_url)/restart

11. port diagnose
  • kill -9 $(lsof -ti tcp:4444)
  • netstat -vanp tcp | grep 3000
  • ps -p 1337 -o comm=
  • fuser -k 5000/tcp || true                    - kill process in jenkins, without true jenkins will fail

12. withCredentials/usernamePassword
  • it's a bind credentials to variable feature
  • update ssh key/certificate, and it's converted to env variable

13. schedule run with params
  • parameterizedCron(env.BRANCH_NAME == 'master' ? "0 8 * * * %TARGET_ENV=staging;TEST_GROUP=All;TEST_COVERAGE=Regression" : '')

references
1. my software builds on my computer but not on jenkins

Saturday, 29 August 2015

selenium testing using maven and jenkins

1. jenkins should be up and running
  • download windows installer @http://jenkins-ci.org/
  • dashboard available @http://localhost:8080/

2. java and maven installed
  • download maven @https://maven.apache.org/
  • setup 'PATH' and 'MAVEN_HOME' env variable for maven

3. make sure tests run on local folder first before moving them to jenkins folder


4. standard folder structure using maven (test scripts must be under src/test/java, name must be *Test.java)

    project
        pom.xml
        src
            test
                java
                    yourTest.java

    cd /users/anoop/project
    mvn test
   

5. by default, maven uses the following naming conventions when looking for tests to run
  • **/Test*.java  - includes all subdirectories
  • **/*Test.java  - includes all subdirectories
  • **/*TestCase.java  - includes all subdirectories
for other name patterns of test cases like "sample,java" or "testSuite.xml", or test cases not under src/test/java folder,  need to add maven-surefire-plugin (not dependency) in pom.xml, and configure these test cases

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18</version>
    </plugin>


6. configure testNG test (maven-surefire-plugin)

    <plugin>
        <configuration>
            <suiteXmlFiles>
                <suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
    </plugin>

    <suite name="suite1">
        <test name="regression1">
            <classes>
                <class name="packagename.classname" />
            </classes>
        </test>
    </suite>

mvn test -DxmlPath=c:/some/path
//be careful that package name in testSuite.xml is the package name in java file, not necessarily the "test/java/..." path name


7. rerun failed testNG test (maven-surefire-plugin)

    <suiteXmlFiles>
        <suiteXmlFile>src/test/resources/testng/testng.xml</suiteXmlFile>
        <suiteXmlFile>target/surefire-reports/testngfailed.xml</suiteXmlFile>
    </suiteXmlFiles>


8. include junit test with different name pattern or exclude junit test (maven-surefire-plugin)

   <configuration>
     <includes>
       <include>sample.java</include>
     </includes>
   </configuration>

   <excludes>
     <exclude>**/TestCircle.java</exclude>
     <exclude>**/TestSquare.java</exclude>
   </excludes> 


9. to run testNG or junit test in parrallel

    <plugin>
        <configuration>
            <parallel>methods</parallel>
            <threadCount>10</threadCount>
        </configuration>
    </plugin>


reference:
1. selenium jenkins - how to do it yourself and the sauce labs advantage
2. my software builds on my computer but not on jenkins
3. maven surefire plugin
4. maven goals and options
5. testing a web application with selenium 2 
6. maven does not find junit tests to run

HTML Tag and Attribute



html element contain <start tag> content <end tag>, e.g. <h1>adam's to do list</h1>
also can add attribute into start tag, e.g. <link href="bootstrap.css" rel="stylesheet" />
css is a style sheet language

tag
----
1. block element vs inline element
block element start on a new line and take full width available, e.g. div
inline element does not start on a new line and only take space necessary, e.g. span

2. div
block level container

3. span
inline level container

4. ul
unsorted list

5. li
List item

6. a
hyperlink

7. select
drop-down list

8. table

9. th
table header cell

10. tbody
container of rows

11. tr
table row

12. td
table cell

11. script
a client side script, such as javascript
src attribute: specifies the url of an external script file


attribute
-----------

selenium locator

1. id (unique in page)
<form id="loginForm">
css: form#loginForm


2. name
<input name="username" type="text" /> "
css: input[name=email]'


3. class name
<p class="content">Site content goes here.</p>
css: p.content


4. tag name
<h1>Welcome</h1>
css: h1


5. link text/ partial link text
<a href="continue.html">Continue</a>
css: a[href="continue.html"]


6. attribute and value
element_name[<attribute_name>='<value>']
<input type="text" id="fistname" name="first_name" class="myForm">


7. xpath
<html>
     <body>
          <form id="loginForm">
               <input name="username" type="text" />
               <input name="password" type="password" />
               <input name="continue" type="submit" value="Login" />
               <input name="continue" type="button" value="Clear" />
          </form>
     </body>
<html>

xpath: form[@id='loginForm']
xpath: //form[@id='loginForm']/input[1]                              //select from anywhere
xpath: //input[@name='continue'][@type='button']

<input type="radio" name="radio-choice-1">

xpath: input[type=’radio’]
xpath: input[name^=’radio’] //prefix
xpath: input[name$=’-1’] //suffix
xpath: input[name*=’choice’] //substring


8. advantage of css selector over xpath
  • developers are more aware about css selector than xpath
  • xpath is implemented differently among browsers

9. analyze complex web page
  • in firefox, install firebug and firefinder, 'right click' on element to find element -> use 'firefind element' to visually verify result from css selector or xpath
  • in firefox, enable firebug, select html panel, click 'click an element in the page to inspect' button -> move around in webpage ui -> find interested element -> right click will lock its html -> move around to inspect nearby ui.
  • right click on ui element in firefox or chrome, if 'this frame' or 'view frame source' option is available, then this element is inside a frame
  • in chrome developer tool 'element' tab, can search (ctrl+f) css selector or xpath, if find it will show in 'element' and ui (when hover in element)
  • in chrome developer tool, the arrow icon on the left hand side is the inspector icon, it can inspect element even in mobile view
  • in chrome developer tool 'console' tab, use $x(".//header") to validate xpath and use $("header") to validate css selector
  • use chrome extension 'copy css selector' to get locator

  • to inspect dynamic element, use setTimeout(function(){debugger;}, 5000) to freeze screen, then click the left icon in 'elements' tab (the icon's name is 'select an element in the page and inspect it' if you hover on it)
  • to inspect an element that cannot be right clicked to inspect, also can click the left icon in 'element' tab

  • $$ find all occurrence (), then each identified by element.all(by.css('.selector')).get(0)etc
  • $ find the 1st occurrence 

10. css syntax
      div >  p                           //all 'p' children whose direct parent is 'div'
      div p                               //all 'p' inside 'div'
      input                               //tag
      .myclass                         //class
      #id                                  //id
      [attribute='value']           //attribute
      escape special char .   #r1\:0\:crmodurlid => #r1\\:0\\:crmodurlid


11. (/bookstore/book[@location='US'])[1]  //select 1st element with specific attribute in xpath


12. frame and iframe
  • $$('iframe')                     
  • show all frames under current 'frame', current 'frame' can be 'top', or can be other iframe 
  • when inspect iframe, right click will show 'view frame source'

  • when inspect iframe, console->context dropdown will show current iframe, and all operation are under current iframe


13. regex locator
  • $("[id*='end']")             //id that end with 'end'
  • $("[class^='begin']")     //class that begins with 'begin'


Reference
1. css selector reference
2. verify css selector or xpath locator
3. css selector practice game
4. firebug: white to black belt
5. evaluate css selector and xpath in chrome developer
6. css selector with examples
7. 1st element of specific attribute in xpath
8. css selector youbube
9. freeze screen in chrome debugger / DevTools panel for popover inspection?
10. use regex in css
11. frames iframes protractor
12. handle iframe in selenium
13. find all iframes on webpage
14. protractor element.all(locator).get(index)





Thursday, 27 August 2015

selenium testing using eclipse/java


1. support for browsers other than firefox (chrome, ie, safari ...)
  • take chrome for example
  • chrome driver is a separable executable that webdriver use to control chrome
  • download chrome driver @http://www.seleniumhq.org/download/
  • change code accordingly when using browsers other than firefox
  • selenium api communicate with webdrivers (ie, firefox, chrome etc) rest service via json wire protocol

2. using testng
  • install testng on eclipse (help -> install new software @http://beust.com/eclipse
  • more flexible in annotation, parametrization, grouping and report than junt
  • easy for parallel execution
  • in testng, a test suite is a 'test***.xml', which can contain java class or package 
  • if using testng with eclipse, can specify 'parallel' in 'test***.xml'
       <suite name="suite1" verbose="1" parallel="methods" thread-count="5">
           <test name="regression1">
               <classes>
                   <class name="NoPackageTest" />
               </classes>
            </test>
            <test name="regression2">
                <packages>
                    <package name="test.sample" />
                </packages>
            </test>
       </suite>

3. using junit
  • using 'Suite' as test runner
      import org.junit.runner.RunWith;
      import org.junit.runners.Suite;

      @RunWith(Suite.class)
      @Suite.SuiteClasses({
             TestFeatureLogin.class,
             TestFeatureLogout.class,
             TestFeatureNavigate.class,
             TestFeatureUpdate.class
      })


4. using maven
  • maven is a java build tool
  • maven manage jar files via pom.xml
  • maven manage build and test
  • maven can execute junit or testng test cases (may or maynot need surefire plugin) 
  • all maven tests of project must be under src/test/java folder
  • by default maven uses the following naming conventions when looking for tests to run under all subdirectories of src/test/java: Test*, *Test, *TestCase
  • install maven on eclipse (help -> install new software @http://download.eclipse.org/technology/m2e/release/)
  • in some selenium and browser version combination, only add
       <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.45.0</version>
        </dependency>

       is not enough, will still have build error, need to add

        <dependency>
            <groupId>com.google.guava</groupId>Maven Standard Directory Layout
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>


        as well


5.  data driven
  • testng @DataProvider annotation
  • apache poi (java api for microsoft office document) to read/write from excel

6. eclipse
  • new folder vs. new source folder vs. new package
  • new source folder add folder to buildpath, new package create the package and the source folder

7. phantomjs
  • use phantomjs-maven-plugin
  • it will download phantomjs, put the path to executable in a phantomjs.binary property file

references
1. testng
2. maven surefire plugin using testng 
3. maven surefire plugin using junit
4. why do we need IEdriver and ChromeDriver but no Firefox driver?
5. selenium webdriver architecture
6. maven standard directory layout
7. difference between “new folder”, “new source folder” and “new package”?
8. java project with cucumber, testng and maven