Showing posts with label automation. Show all posts
Showing posts with label automation. Show all posts

Thursday, January 23, 2020

One file example to run data driven tests using python and nose with XML and HTML reports

I was discussing data driven tests with a colleague and how easy it to create in python just with nose test framework.

The file looks like:

def test_dirs():
    for data in test_data():
        yield check_dir, data[0], data[1]

def check_dir(dir_ref, dir_new_gen):
    assert len(dir_ref) > 1
    assert len(dir_new_gen) > 1

def test_data():
    return [["dir1_ref", "dir1_gen"], ["dir2_ref", "dir2_gen"]]

I created a repository  and shared with him following is current content of the Readme:

https://github.com/mubbashir/py_nosetest_data_driven

One file example to run data driven tests using python and nose tests with XML and HTML reports

Documents

Optional VENV (Python virtual environment)

  • Create virtual environment python -m venv venv
  • Activate virtual env
    • Linux/Mac source venv/bin/activate
$ source venv/bin/activate
(venv) [akhan@seq-akh-fc pipeline_workflow_tests] which python
~/workspace/pipeline_workflow_tests/venv/bin/python

Install and execute

  • Install dependencies: pip install -r requirements.txt
  • Run tests
$nosetests -v
nose_test_generator.test_dirs('dir1_ref', 'dir1_gen') ... ok
nose_test_generator.test_dirs('dir2_ref', 'dir2_gen') ... ok
nose_test_generator.test_data ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.003s

OK

Run with reports

$nosetests -v --with-xunit --xunit-file=nose_test_results.xml --with-html-output --html-out-file=nose_test_results.html
nose_test_generator.test_dirs('dir1_ref', 'dir1_gen') ... ok
nose_test_generator.test_dirs('dir2_ref', 'dir2_gen') ... ok
nose_test_generator.test_data ... ok

----------------------------------------------------------------------
XML: /home/akhan/workspace/py_nosetest_data_driven/nose_test_results.xml
----------------------------------------------------------------------
Ran 3 tests in 0.002s

OK

Tuesday, April 9, 2019

Gradle task to check files/commands in PATH environment variable before running tests

Recently I was debugging a failing tests and it turn out the when Jenkins runs the tests on a remote node shell environment is not initialized hence one of binary dependency of the application under tests was missing.

Solution:
Add the additional location in the PATH from jenkins
e.g. export PATH=$PATH:/opt/bin/
Execute shell  additional path
Force checking in gradle:
In the light of principle like ""treat test code with the same level of care as production code" and To help my fellow tester (and my future self) it's better to add this as check in gradle.
// rest of your build
// Verfied with gradle 4.10
task("checkEnv"){
    doFirst {
        def listOfFileToCheckInPath = ['find', 'grep']
        listOfFileToCheckInPath.each { file ->
            if(!isFoundInPath(file))
                throw new GradleException("${file} was not found in any of the folder in PATH: ${System.getenv('PATH').split(File.pathSeparator)}")
        }
    }
}
/**
 * Static function to verify if a file/command exist in PATH environment 
 * @param file
 * @return true if found, else false
 */
def static isFoundInPath( file){
    def PATH_ENV = System.getenv('PATH')
    def fileFound = PATH_ENV.split(File.pathSeparator).find{ folder ->
        println("Looking for ${file} in ${folder}")
        if (Paths.get( "${folder}${File.separator}${file}").toFile().exists()){
            println("Found ${file} in ${folder}")
            return true
        }
    }
    return fileFound
}
// Making test task to depend on checkEnv
test.dependsOn checkEnv
The output would look something like:
./gradlew checkEnv
> Task :checkEnv
Looking for find in /usr/local/bin
Looking for find in /usr/bin
Found find in /usr/bin
Looking for grep in /usr/local/bin
Looking for grep in /usr/bin
Found grep in /usr/bin

BUILD SUCCESSFUL in 0s
Gist on github

Tuesday, October 6, 2015

Parallel execution of automated check via selenium, behave and docker


We were looking for a solution for executing selenium bases scenarios written in behave in parallel to speed up the execution time. Unluckily behave doesn't have this in build like cucumber  fortunately we came across https://github.com/hugeinc/behave-parallel  in which  upstemsync was just couple of weeks old :)

I already had a picture in mind how would the entire step would work so creating  a demo project wasn't too difficult.

Check out https://github.com/mubbashir/behave-selenium 
Following is screen recoding where I had connected to both nodes via VNC and checks were executed in Parallel  on features level


Links:
https://github.com/hugeinc/behave-parallel
https://github.com/behave/behave/
https://github.com/SeleniumHQ/docker-selenium

Following is what the readme says at the moment:

behave-selenium

Selenium's example using behave
A quick example of python, behave, selenium, webdriver and docker to run prallel tests
  • Install docker and create a selenium grid with say 2 nodes
    • docker run -d -p 4444:4444 --name selenium-hub selenium/hub:2.47.1 ## To Run the Hub
    • docker run -d -P --link selenium-hub:hub selenium/node-firefox-debug:2.47.1 ## Run it twice to create 2 nodes, Docker will pick a random name
    • Check the running docker containers
$ docker ps
CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                     NAMES
34911106818f        selenium/node-firefox-debug:2.47.1   "/opt/bin/entry_point"   2 hours ago         Up 2 hours          0.0.0.0:32769->5900/tcp   condescending_elion
14971ef95408        selenium/node-firefox-debug:2.47.1   "/opt/bin/entry_point"   2 hours ago         Up 2 hours          0.0.0.0:32768->5900/tcp   trusting_mahavira
10688bf744d4        selenium/hub:2.47.1                  "/opt/bin/entry_point"   3 hours ago         Up 3 hours          0.0.0.0:4444->4444/tcp    selenium-hub
  • Selenium gird console to have two nodes http://192.168.99.100:4444/grid/console # if you are on mac or on windows get the ip of the running docker vm e.g. docker-machine ip default
  • Get port of VNC if you want to connect to a node
docker port <container-name|container-id> 5900
docker port 34911106818f
#=> 0.0.0.0:49338
  • Clone the repo
  • Create virtulenv $virtualenv env
  • Activate the virtualenv and run the requirements file via pip source env/bin/activate; pip install -r requirements.txt
  • Export GRID_HUB_URL export GRID_HUB_URL='http://192.168.99.100:4444/wd/hub'
  • Run behave --processes 2 --parallel-element feature

Contributing

  • Run the requiremetns file in the virtual environment
  • Run pre-commit install ## To install pre-commit
Run pre-commit install to install pre-commit into your git hooks. pre-commit will now run on every commit. Every time you clone this project running pre-commit install should always be the first thing you do.
If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files. To run individual hooks use pre-commit run <hook_id>.
The first time pre-commit runs on a file it will automatically download, install, and run the hook. Note that running a hook for the first time may be slow. For example: If the machine does not have node installed, pre-commit will download and build a copy of node.
  • To Run all pre-commit manually on all file type pre-commit run --all-files
  • To run specific hook (via the id in .pre-commit-config.yaml) on specific file(s) pre-commit run flake8 --files bdd_feature_tests/features/steps/dubizzle_homepage_steps.py
  • To run autopep 8 manually (which will run automatically once pre-commit is configured) type pre-commit run 'autopep8-wrapper' --files bdd_feature_tests/features/steps/dubizzle_homepage_steps.py


Wednesday, September 30, 2015

Flashback: Intro to Selenium and Automation- Slide deck

Flashback -  Today while going through some documents on my google drive I came across this presentation which I have used in the past back to give very basic introduction to selenium and automation.



Monday, September 27, 2010

Ruby IRB Auto Completion and Highlight - Linux

Install wirble

laptop:~$ sudo gem install wirble
Successfully installed wirble-0.1.3
1 gem installed
Installing ri documentation for wirble-0.1.3...
Installing RDoc documentation for wirble-0.1.3...

vim ~/.irbrc
Put the following in this file:

require "rubygems"
require "wirble"
Wirble.init
Wirble.colorize

IRB.conf[:AUTO_INDENT] = true
IRB.conf[:USE_READLINE] = true
IRB.conf[:LOAD_MODULES] = [] unless IRB.conf.key?(:LOAD_MODULES)
unless IRB.conf[:LOAD_MODULES].include?('irb/completion')
IRB.conf[:LOAD_MODULES] << 'irb/completion'
end


type
:wq

Thats it on irb prompt use tab for auto completion

Example:

Install random_data

laptop:~$ sudo gem install random_data
Successfully installed random_data-1.5.1
1 gem installed
Installing ri documentation for random_data-1.5.1...
Installing RDoc documentation for random_data-1.5.1...

-laptop:~$ irb
>> require 'rubygems'
=> false
>> require 'random_data'
=> true
>> Random.al
Random.allocate Random.alphanumeric
>> Random.alphanumeric 11
=> "w1KbPVLzE2l"

Tuesday, September 25, 2007

Fix Firefox "Unresponsive Script" warnings

Recently i was facing this problem while writing some Automated Web Functional testing using selenium that firefox pops a warning for unresponsive script (for some how i had to use Thread.sleep(1000)).

This is not only annoying but also hearts the efforts for producing unattended scripts.

Google again come to rescue and i got this from. So the solution goes like:
Go the hidden configuration page in Firefox by typing about:config in the address bar . (make sure that you are doing this for the profile you are using for selenium)
In the 'Filter' box, type script_run_time. This will narrow the options to dom.max_script_run_time and dom.max_chrome_script_run_time.

Right-click it and choose Modify. A box pops up. Change the number to something bigger like 40. This is the maximum time a script can run before Firefox considers it 'unresponsive'.

If you can’t find the string in the about:config page, create it by right-clicking anywhere and then choose New—> Integer and enter there name.