Showing posts with label Chrome. Show all posts
Showing posts with label Chrome. Show all posts

9/10/2021

Visual Studio Code, WebdriverIO, JavaScript and Chrome

 For this setting I will use the great article: WebDriverIO Integration With Cucumber.

1. npm init -y

2. npm i --save-dev @wdio/cli

3. npx wdio config


4. npm install @wdio/allure-reporter --save-dev

5. npm install -g allure-commandline --save-dev

6. npm install --save moment

7. Create features directory with pages and step-definitions subdirectories. Create reports directory.


8. The first test scenario will be the same, so create first.feature file in features directory:

Feature: Automation Playground blog
As a test automation enthusiast
I want to write test for Automation Playground blog
So that I can practice Cucumber

Scenario: Verify the blog's title
  Given the Automation Playground blog open
    Then the Automation Playground blog title should be equal <title>
     Examples:
            |title                   |
            |"Automation Playground" |

9. WebdriverIO provides ability to easily create page objects, so create page objects in pages directory. The base page object:

module.exports = class BasePage {
        open (path) {
            browser.url(path);
        }
    }

Page object for the Home page:
const BasePage = require('./base.page')

const base_url = 'https://automation-playground.blogspot.com/'

class HomePage extends BasePage {
    open() {
        super.open(base_url)
    }
}

module.exports = new HomePage();

10. Create steps definition file in the corresponding directory. The content will be slightly differ from the previous setup:

const { Before, Given, When, Then } = require('@cucumber/cucumber')
const HomePage = require('../pages/home.page');

Given('the Automation Playground blog open', function () {
  HomePage.open()
});

Then('the Automation Playground blog title should be equal {string}', async function (title) {
  expect(browser).toHaveTitle(title);
});

11. Edit wdio.conf.js file according to the article

afterStep: function (step, context, { error, result, duration, passed, retries }) {
        if(error) {
            browser.saveScreenshot('./reports/screenshots/Fail_' + 
                                   moment().format('DD-MMM-YYYY-HH-MM-SS') + '.png')
        }
     },

and

    reporters: [['allure', {
            outputDir: './reports/allure-results'
        }]],   

and

   cucumberOpts: {
        // <string[]> (file/dir) require files before executing features
        require: ['./features/step-definitions/first.steps.js'],

and put on the top of the config file the following:

const moment= require('moment')

12.

And the scripts in package.json :
  "scripts": {
    "test:bdd": "npx wdio run ./wdio.conf.js",
    "generate:allure" : "allure generate reports/allure-results/ --clean"
  },

12. Run the test: 

npm run test:bdd


13. On my Windows computer I have to run cmd as Administrator, go to the folder and execute the following commands to generate and open Allure test report:

 allure generate reports/allure-results/ --clean

and then:

 allure open

The test report appears in browser:



9/06/2021

Visual Studio Code, selenium-webdriver, JavaScript, Cucumber and Chrome

Now we will use selenium-webdriver and Cucumber.js for end-to-end testing for the same test scenario.

(Be aware: selenium-webdriver and webdriver.io are not the same.)

I will use Visual Studio Code as a code editor again and Windows system.

For better Cucumber support you may install the following VSC extensions (Ctrl + Shift + X):

  • Cucumber (Gherkin) Full Support
  • Search and install 'Snippets and Syntax Highlight for Gherkin (Cucumber)

1. Create a directory for the project, initialize the node project in the directory:

npm init -y

2. Then install selenium-webdriver :

npm install selenium-webdriver --save-dev

3. Install cucumber.js :

npm install --save-dev @cucumber/cucumber

4. Then install chromedriver:

npm install chromedriver --save-dev

5. Install cucumber-html-reporter :

npm install cucumber-html-reporter --save-dev

So, the dev dependencies part in package.json looks like:


6. Add the following script:

"test": "./node_modules/.bin/cucumber-js features -f json:report/cucumber_report.json"


7. Create features directories with support and steps subdirectories:


8. In support folder create env.js file:

const { setDefaultTimeout } = require('@cucumber/cucumber');
setDefaultTimeout(60 * 1000);

9. In support folder create word.js file:

require('chromedriver');

const { setWorldConstructor } = require('@cucumber/cucumber');
const webdriver = require('selenium-webdriver');
const Capabilities = require('selenium-webdriver/lib/capabilities')
        .Capabilities;
const capabilities = Capabilities.chrome();

function World({ attach, parameters }) {
    this.driver = new webdriver.Builder()
    .withCapabilities(capabilities).build();
}

setWorldConstructor(World);

10. In the project "root" directory create index.js file:

const reporter = require('cucumber-html-reporter');
 
const options = {
        theme: 'bootstrap',
        jsonFile: 'report/cucumber_report.json',
        output: 'report/cucumber_report.html',
        reportSuiteAsScenarios: true,
        scenarioTimestamp: true,
        launchReport: true,
        metadata: {
            "App Version":"0.0.1",
            "Test Environment": "STAGING",
            "Platform": "Windows 10",
            "Executed": "Local"
        }
    };
 
    reporter.generate(options);

11. In features directory create first.feature file with the first test scenario written in Gherkin language:

Feature: Automation Playground blog
As a test automation enthusiast
I want to write test for Automation Playground blog
So that I can practice Cucumber

Scenario: Verify the blog's title
  Given the Automation Playground blog opened
    Then the Automation Playground blog title should be equal <title>
     Examples:
            |title                   |
            |"Automation Playground" |

12. In steps directory create first.steps.js file with the steps difinitions:

const { Before, Given, When, Then } = require('@cucumber/cucumber')
const assert = require('assert')

const base_url = 'https://automation-playground.blogspot.com/'

Given('the Automation Playground blog opened', function () {
  return this.driver.get(base_url);
});

Then('the Automation Playground blog title should be equal {string}', async function (title) {
  const actualTitle = await this.driver.getTitle();
  assert(title, actualTitle);
});

13. Also in steps directory create hooks.js file:

const { After, Before } = require('@cucumber/cucumber');

After(function(){
        return this.driver.quit();
  })

14. In the project "root" directory create report subdirectory with empty cucumber_report.json file

15. Run the test:

npm test

The following test scenario will be executed: 
  •  run Chrome browser
  •  go to https://automation-playground.blogspot.com
  •  verify the blog's title
  •  close Chrome browser


 The test passed!

16. Execute the following command to create html report from now populated cucumber_report.json:

node index.js



3/07/2021

Visual Studio Code, Nightwatch.js, JavaScript and Chrome

For now we will use Nightwatch.js for end-to-end testing.

I will use Visual Studio Code as code editor and Ubuntu system.

Create a directory for the project, initialize the node project in the directory:

npm init -y

Then install nightwatch.js:

npm install nightwatch chromedriver --save-dev


First nightwatch run without arguments to generate nightwatch.conf.js file: 

npx nightwatch

Create a directory for tests and write its location to the config file:


 Create a test file with the same test as in Visual Studio Code, Selenium, Firefox, JavaScript and Mocha with Chai article. 

We will verify the blog title.

module.exports = {
    "assert title": browser => {
        browser.url("https://automation-playground.blogspot.com");

        browser
            .assert.title("Automation Playground");
    }
}

To run the test execute:

npx nightwatch -e chrome

The test passed!


 Nightwatch.js allows us to use BDD style.

Let's use describe(), before(), test() and after() to better structure our test; you can copy/paste the example:

describe('Testing Automation Playground blog', () => {

    before(browser => {
        browser
            .url("https://automation-playground.blogspot.com");
    });

    test("Verify the blog title", browser => {
        browser
            .assert.title("Automation Playground");
    });

    after(browser => {
        browser
            .end();
    });
});

npx nightwatch -e chrome

The following test scenario will be executed again:

  • run Chrome browser
  • go to https://automation-playground.blogspot.com 
  • verify the blog's title
  • close Chrome browser

The test passed!



Visual Studio Code, WebdriverIO, JavaScript and Chrome - cucumber html test report

 Apart Allure test report  we can use Cucumber test report in html format. We will follow this instructions . 1. Install  wdio-cucumberjs-js...