Dieses Blog durchsuchen

Montag, 8. August 2016

docker: create your first container with selenium 3.0 on ubuntu 16.04



In the last post, we installed docker on our local ubuntu 16.04

Now let's create an own image with a complete selenium testenviroment on it.

Prerequisits

Installed Docker
An account on docker hub (https://www.docker.com/)

Pull an Ubuntu 16.04 image

We want to install Selenium on a lunux ubuntu image. So we have to pull this image first.

$ sudo docker pull ubuntu:14.04
$ sudo docker images

The first command downloads the ubuntu base image, we want to use.
The second command "images" shows us the newly installed image. Please remember the Image ID. You will need it now




Create a new container


Use Run to create a new container
"Run" is used to create a new container.

You can type:
$ sudo docker run --help
to get all supported options.

For our first image we want to use:
$ docker run-i -t <your ubuntu image id> /bin/bash

This tells docker to create a container and open a shell on it, so that we can install our selenium tools.

Now you have a console on the ubuntu machine.

You can type "exit" to get back to the host.

Start the new container

After you have created your container, you can list all containers with
$ docker ps

This will output your running processes:

You now see your container id in the left

To start our container we type:
$ docker start <your container id>

Bind a shell to the container

At nex we want to bind a shell on our new container with:
$ docker attach <your container id>

The next input will be on the container

Install Selenium 3.0

After we have binded a shell on the new container, we install our testenviroment on the container
$ apt-get update

  
Install Java
$ apt-get install openjdk-8-jdk

This could take a while.....

Install wget
$ apt-get install wget


Download and install selenium
$ mkdir /usr/lib/selenium
$ wget --output-document=selenium-server-standalone-3.0.0-beta2.jar http://goo.gl/2lZ46z
$ chmod +x selenium-server-standalone-3.0.0-beta2.jar

This will download the selenium server and set executable accessrights


You should have a look for the current downloadurl of selenium under
http://www.seleniumhq.org/download/

Install Selenium as a service


To install the new selium server as a service you only have to save follwing script under "/etc/init.d/selenium"



Make script rebootsave
$ update-rc.d selenium defaults

Create logfiles
$ mkdir -p /var/log/selenium
$ touch /var/log/selenium/selenium-output.log

Install gecko webdriver for firefox

$ cd /usr/lib/selenium
$ wget https://github.com/mozilla/geckodriver/releases/download/v0.10.0/geckodriver-v0.10.0-linux64.tar.gz
$ tar xfvz geckodriver-v0.10.0-linux64.tar.gz
$ mv geckodriver-v0.10.0-linux64.tar.gz geckodriver


Install firefox

$ apt-get install firefox

Install webdriver for chrome

$ cd /usr/lib/selenium
$ wget http://chromedriver.storage.googleapis.com/2.23/chromedriver_linux64.zip
$ apt-get install unzip
$ unzip chromedriver_linux64.zip
$ rm -rf  chromedriver_linux64.zip


Install headless browsing

To use headless browser we need xvfb. Headless means, that our server has no UI like a desktop machine. So the browser needs to be started "headless"
$ apt-get install xvfb
$ Xvfb :10 -ac
$ export DISPLAY=:10




Test selenium server

To test, if the serviceinstallation worked, you can simply type:

$ /etc/init.d/selenium start


Commit your container to the docker hub

 Your installation on the container is now complete. You now can commit and push it to docker

$ docker login

Now you have to enter you accessdata

$ docker commit -m "add selenium server" <your dockerid><your imageid>
$ docker push <your dockerid>

Thats it. Your new image is avalable in the docker hub

install docker on ubuntu 16.04



Docker is the most common virtualisation software in the linux world and it's widly used in the webworld. To explain what docker is, seems to be a little off topic. So here is how to install it.

If you want to have some deeper inforamtion about docker, you can follow this link: 
https://www.docker.com/what-docker

Prerequisits

Install a nonrootuser with sudo rights
usermod -aG sudo <your username>

Installation

Update your APT
$ apt-get update

Add a GPG Code  to access Docker Reposotory
$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 
58118E89F3A912897C070ADBF76221572C52609D

Add docker to the apt sources
$ echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list
$ apt-get update 
 

>Install app
$ apt-get install -y docker-engine

Test status
$ sudo systemctl status docker

it should output something like that:


Thats it. Docker is running on you machine.

Samstag, 6. August 2016

javascript: unittesting with node.js, mocha and chai



In the  last post, we had setup a unittest enviroment for node.js with mocha and chai. Today we want to use it on our testproject

Prerequisits

You have to be installed:
Node.js
Chai.js
Mocha.js

Our project from the last post:
$ git clone https://github.com/pboethig/starwarsNames.git
$ npm install

Let's start


This was our code to test. File "src/index.js"
If you open that file, you see that we have 2 properties in our module. "starwarsNames" from type String Array and "random" from type Random String. Now we want to test this 2 types.

For that we implement a function "isTypeOfStringArray()" and the describe function to test that in our testfile under "src/index_test.js"

Here is my "src/index_test.js" You deeply understand how the testcode works, if you have a look on the chai documentation
 


Run the test

Now switch to your terminal and type:
$ npm t

This will launch the configured testfile "src/index_test.js".We have configured it in "package.json"

Now you should see something like that
 
 Great. The test was successful.

Let's create another test

Now we want to test, if "Luke Skywalker" is in the list with starwarsnames

I have added the new test to "src/index_test.js"
As you can see, I have added a second test to the describe "starwars-names" function. This tests uses "to.include()" to check the array for the string "Luke Skywalker". As you can see, you only need to save the testfile, and the test gets executed directly.

Kent You should now see a terminal like that:

Awesome. That it!


And now the final test

The last test, checks if the "random" function of our library gives us a random starwars-name from the list

I have added the test "should contain a random name from the list"to the tests.

Here is my final version of "src/index_test.js"
If you run the test, you should see 3 passed tests now
Thats all for now. Thanks to Kent.C.Dots for the great video:
https://egghead.io/lessons/javascript-how-to-write-a-javascript-library-unit-testing-with-mocha-and-chai

javascript: setup unittesting with mocka and chai on a node.js module. Building a bridge



In our last post, we had setup a node.js module and a npm project.
On this project we now want to setup the enviroment for some unittesting.

Prerequisits

1) Node.js
2) Npm 
3) gitclient

If you did not follow the tutorial, you can checkout the project here:
Open a terminal and type:
$ git clone https://github.com/pboethig/starwarsNames.git
$ npm install

Otherwise:

Add unittesting libraries

At first we want to add 2 new node libraries "mocha" and "chai" to our project to bring the unittesting capabilities.

Open a terminal and navigate to your projectroot and type:
$ npm install --save-dev mocha chai

After that you have the 2 new libraries in your local/node_modules


Create a first dummy-test

- create a file named "index_test.js" in the folder "src"
- open this file in your IDE.  I am using vscode with node.js plugins for that.

In this testfile we add follwing code.

To explain the code. At first we are referencing the expect method to a local variable "expect". In the second line, we are referencing our main module file "index.js", which we want to test.

The next lines are our first simple test, in which we want to try, if the testframeworks are loaded correctly.

Enabling the test in you package.json


At next we have to extend our package.json to setup our testfile.
- open the package.json
- search the line:
"test": "echo \"Error: no test specified\" && exit 1"
and replace it with
"test": "mocha src/index_test.js -w"

This tells npm to start the testfile under "src/index_test.js" with_ the mocha library.

Run your first test


Now you can test that by executing.
$ npm test

in your projectroot.

Yeah your first test is running, if you see something like this:


Source: https://egghead.io/lessons/javascript-how-to-write-a-javascript-library-setting-up-unit-testing-with-mocha-and-chai. Thanx Kent.C.Dotts for the great video.




Freitag, 5. August 2016

create a tag in git and release this tag

Today we want to create a repo, add a file and create a tag. After that, we want to release this tag.

This is a everyday process you need, if you want to publish a new version of your software,

Prerequisits:
- a git client 
- a github account

1) create a repository named "testing-tags" on github. If you do not have an account, you have to create one on github.com


2) Create a local folder "testing-tags" somewhere on your computer
 
- open a console an navigate to this folder and type:
 
echo "# testingtags" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/<youtgithubusername>/testing-tags.git
git push -u origin master
 
This will add the README.md to the new github repository.
 
 
Now we want to tag the first version of your software 
in your terminal type:
git tag 1.0.0
git push --tags
 
Thats it. You have pushed your first tag 1.0.1 to github you can find it on github here:
 
 

javascript: create an npm package with a simple node.js module on ubuntu

Today I want to create a npm package for a simple javascriptlibrary with modules and some tests. Src:https://egghead.io/lessons/javascript-how-to-write-a-javascript-library-configuring-npm-and-creating-a-package-json

At first we need a git repository on github.

Create a repository named "starwarsNames"

Login into your github account and create a new repository "starwarsNames"

echo "# starwarsNames" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/pboethig/starwarsNames.git
git push -u origin master

create an account on https://www.npmjs.com, if you dont have one
You will need an account there to publish your extension. 

Remember your <npm-username> you will need it.

Install nodejs
$ apt-get install nodejs
$ sudo ln -s /usr/bin/nodejs /usr/bin/node  

Install npm, if npm is not installed 
$ apt-get install npm 


If you are on windows you have to install it manualy, If you are on mac, you can use the packagemanager,

At next we inititialize the npm project. 
$ npm set init-author-name 'Your Name'
$ npm set init-author-email 'your email'
$ npm set init-author-license 'MIT'
$ npm set save-exact true

If you want to check that config, you can do it with
$ cat ~/.npmrc

Add your npm user to your project 
$ npm adduser
Username: <your npm-username>
Password: <your npm-password>
Email: (this IS public) <your npm-email>

Initilize your npm project
$ npm init

this will ask some data from you, and puts outs the package.json for you

name: (starwars-names-<your npm username>)
version: (1.0.0)
license: (MIT)
About to write to /var/www/html/starwarsNames/package.json:

{
  "name": "starwars-names",
  "version": "1.0.0",
  "description": "get random starwars names",
  "main": "src/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+ssh://git@github.com/pboethig/starwarsNames.git"
  },
  "keywords": [
    "random",
    "starwars"
  ],
  "author": "pboethig",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/pboethig/starwarsNames/issues"
  },
  "homepage": "https://github.com/pboethig/starwarsNames#readme"
}






 


Okay. Lets create our first module:
- create a folder "src" in your projectroot to store our module sources.
- now , create a file "index.js" in the new src folder

Open this new file in your code editor. I am using vscode for it. That editor  has great node.js plugins.

Now we want to define our modulestructure. 
Type following code in index.js

This tells node that you want to store the "starwars-names.json" list in a variable, that your module will use and it exports 2 properties ("all" and "random")

At next save the starwarsnames in a file "starwars-names.json" in your "src" folder:


At next we need a further npm library called unique-random-array.
Lets install it with:

$ npm install --save unique-random-array

This will save the new library to our dependency definition in package.json

Now lets use the new array library in our index.js

Our index.js now looks like that
 
Now that we have the library included, lets use it in our module.

In the export function, we want to reference all starwars-names to the property "all" and a random startwars-name to the property "random".

To to that we only have to export the local variable "starwarsNames" to the global scope by referencing the  exportproperty "all" and the array-function uniqueRandomArray(starwarsNames) from our new library to the export-property "random". Here is the code of "index.js".
Now our index.js looks like that
Yeah, we havehacked our first node module.

Lets test it!
- open a terminal and navigate to the root folder of your app 
$ node
$ var starwarsNames = require('./src/index.js');
$ starwarsNames.all
$ starwarsNames.random()  

The single commands will output the whole list of names and a random name from the list. Thats realy great!


Now we want to publish it in npm as an own package. 
For that, we have to commit it to our git repository.

open a terminal:
$ cmd to your projectroot
$ touch .gitignore
$ nano .gitignore

add "node_modules" to your .gitignore to prevent commiting the node_modules.

$ git add *
$ git commit -m "init"
$ git push origin master

Thats it.

Now we can publish it to npm.
Thats realy simple.

Just type:
$ npm publish

That it. Now you should directly see your new package on npm !

This is awesome!

Now we want to test, if our module is directly usable via npm.
- open a terminal to the desktop 
$ npm install starwars-names-<your npm_user> 

Super. Now you can use your library, from anywhere in the world.
switch to the desktop and create a file named "test.js" with following content:
Now run a terminal and cd to the directory where you have stored test.js and run:
$ node test.js

You will now see the results from your library.

This is realy cool!


Thanky to Kent.C.Dotts for the great video!

In the next lesson, we want to extend the simple sample with unit tests.



Selenium 3 and phpunit with facebooks webdriver

Today, we want to extend our simple test, in wichh we have loaded a page and compared its title,  to a litte more comlex test.

If you do not have the prerequisits installed, you can follow the previous tutorial here:
http://magento2-tuts.blogspot.de/2016/08/phpunit-simple-tests-with-selenium.html





Our goal for today is to:

1) Setup and configure the webdriver, so that all tests are running in Firefox and chrome.

2) execute our test which fills a searchform and submits it to the server. Gets the searchresult and schecks the result for a specific string

3) closes the connection to the webdriver

4) quits the browser.

sounds interresting or?


Lets start with the setup:

As you can see in the setUp() method, the driver gets simply configured. You can setup a list with browsers, which are used for testing by the webdriver.


Lets repeat a simple test.
This test will only load the page and search for a String "GitHub" 


Lets make an automated search.
The next test will automaticly search the searchfield and type i  a serachterm
After that the result gets parsed for the searchstring and gets sserted for it


 
At last we want to sutdown the webdriverconnection and close the window

Thats it. Both tests are green,

You can download  the code here:
https://github.com/pboethig/unittest