2017-05-29 1 views
1

Environnement et approche: - Protractor: 5.1.2 En utilisant POM pour tester deux sites: https://www.angularjs.org/ et https://angular.io/. Deux suites: Test1.js et Test2.js, Config. jscadre Design POM avec Protractor: Page Problèmes de synchronisation

conception Problème: - Lorsque vous utilisez des objets de page pour tester deux sites différents (clics de simple bouton), le premier test ne parvient pas à cliquer sur le bouton et seconde passe (comme indiqué dans les spécifications de fichier de configuration: [ ». /specs/Test1.js','/specs/Test2.js ']). Lorsqu'il est exécuté indépendamment, les deux tests passent! Élément non trouvé exception dans le premier test.

Structure des dossiers: ./pageobjects/Angular.po.js & AngularHomepage.po.js & CommonUtils.po.js ./specs/Test1.js & Test2.js

Fichiers: Config. js

var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter'); 
var reporter = new HtmlScreenshotReporter({   
    pathBuilder: function(currentSpec, suites, browserCapabilities) { 
    return browserCapabilities.get('browserName') + '/' + currentSpec.fullName; 
    } 
}); 

exports.config = {  
    capabilities: { 
    'browserName': 'chrome' 
    }, 

    specs: ['./specs/Test1.js','./specs/Test2.js'], 

    jasmineNodeOpts: { 
    defaultTimeoutInterval: 30000 
    }, 

    // Setup the report before any tests start 
    beforeLaunch: function() { 
    return new Promise(function(resolve){ 
     reporter.beforeLaunch(resolve); 
    }); 
    }, 

    // Setup before every test starts 
    onPrepare: function() { 
    browser.manage().window().maximize(); 
    browser.manage().timeouts().implicitlyWait(5000); 

    // Assign the test reporter to each running instance 
    jasmine.getEnv().addReporter(reporter); 
    }, 

    // Close the report after all tests finish 
    afterLaunch: function(exitCode) { 
    return new Promise(function(resolve){ 
     reporter.afterLaunch(resolve.bind(this, exitCode)); 
    }); 
    } 
}; 

Angular.po.js

var Angularpage = function() { 
     var getStartedBtn = element(by.css('.hero-cta.button-plain.md-button')); 
     this.clickgetStartedBtn = function(button) { 
      getStartedBtn.click(); 
     };   
    };  
    module.exports = Angularpage 

AngularHomepage.po.js

var AngularHomepage = function() { 
     var learnBtn = element(by.css('.learn-link'));  
     this.clickLearnBtn = function(button) { 
      learnBtn.click(); 
      };  
    };  
    module.exports = AngularHomepage 

CommonUtils.po.js

var CommonUtils = function() {   
     this.get = function(url) { 
      browser.get(url,10000); 
      }; 
     this.getTitle = function(title) { 
      return browser.getTitle(); 
      }; 
     this.goBack = function(button) { 
      browser.navigate().back(); 
      };  
    };   
    module.exports = CommonUtils 

Test1.js

//Importing multiple page objects and creating instances 
    var commonUtils = require('./../pageobjects/CommonUtils.po.js'); 
    var utils = new commonUtils(); 
    utils.get('http://www.angularjs.org'); 

    var homePage = require('./../pageobjects/angularHomePage.po.js'); 
    var angularHomepage = new homePage(); 

    describe('Angularjs homepage tests', function() 
    { 
     beforeEach(function() {  
     browser.waitForAngular(); 
     }); 

     //Works with angular and non-angular page 
     it('Should click a button and navigate to non-angularpage', function(){  
     angularHomepage.clickLearnBtn(); 

     //Interacting with non Angular site in between 
     //Wait for page to load and verify the url 
     browser.ignoreSynchronization = true; 
     browser.driver.wait(function() { 
      return browser.driver.getCurrentUrl().then(function(url) {    
       return (/codeschool/).test(url); 
      }); 
     }); 

     expect(browser.getTitle()).toEqual('AngularJS vs Angular | Code School');  
     utils.goBack(); 
     browser.ignoreSynchronization = false;  
     }); 
}); 

Test2.js

//Importing multiple page objects and creating instances 
    var angularPage = require('./../pageobjects/Angular.po.js'); 
    var angularpage = new angularPage(); 

    var commonUtils = require('./../pageobjects/CommonUtils.po.js'); 
    var commonUtils = new commonUtils(); 
    commonUtils.get('https://angular.io/'); 

    describe('Angular IO Page tests', function() 
    { 
     beforeEach(function() { 
     browser.waitForAngular(); 
     }); 

     it('Should click Get started button and navigate to new page', function() {   
     //Button click 
     angularpage.clickgetStartedBtn(); 

     //Wait for page to load and verify the url 
     browser.wait(function() { 
      return browser.driver.getCurrentUrl().then(function(url) { 
       return (/latest/).test(url); 
      }); 
     }); 

     //Verify Page title from common page objects 
     commonUtils.getTitle(function(text){ 
      expect(text).toEqual('One framework. - Angular');   
     });  
     });  
    }); 

exécution Erreur: NoSuchElemen TERREUR: Aucun élément trouvé avec le localisateur: Par (sélecteur de css, .En savoir-link)

Répondre

0

Le problème est dans ces lignes

// Test1.js 
var utils = new commonUtils(); 
utils.get('http://www.angularjs.org'); 

Et

// Test2.js 
var commonUtils = new commonUtils(); 
commonUtils.get('https://angular.io/'); 

Vous devez placer le get ' s dans le beforeEach(). Le beforeEach est utilisé pour "configurer" votre "testenvironment" avant chaque describe. (Également testé pour être sûr et cela fonctionne ;-))

Hope it helps

Mise à jour

Si vous ne souhaitez que l'URL pour charger une fois pour chaque test (it) vous devez utiliser le beforeAll() en vous describe(), vous obtiendrez quelque chose comme ceci par exemple pour votre fichier Test2.js

//Importing multiple page objects and creating instances 
 
var angularPage = require('./../pageobjects/Angular.po.js'); 
 
var angularpage = new angularPage(); 
 

 
var commonUtils = require('./../pageobjects/CommonUtils.po.js'); 
 
var commonUtils = new commonUtils(); 
 

 
describe('Angular IO Page tests', function() { 
 
    // Now it is loaded once for all `it`'s 
 
    beforeAll(function() { 
 
    commonUtils.get('https://angular.io/'); 
 
    }); 
 

 
    beforeEach(function() { 
 
    browser.waitForAngular(); 
 
    }); 
 

 
    it('Should click Get started button and navigate to new page', function() { 
 
    //Button click 
 
    angularpage.clickgetStartedBtn(); 
 

 
    //Wait for page to load and verify the url 
 
    browser.wait(function() { 
 
     return browser.driver.getCurrentUrl().then(function(url) { 
 
     return (/latest/).test(url); 
 
     }); 
 
    }); 
 

 
    //Verify Page title from common page objects 
 
    commonUtils.getTitle(function(text) { 
 
     expect(text).toEqual('One framework. - Angular'); 
 
    }); 
 
    }); 
 
});

+0

Merci pour la réponse.Mais si je veux toutes les navigations une seule fois avant l'ensemble de la suite de tests? beforeEach (browser.get) signifierait qu'il charge la page à chaque fois et pour chaque bloc it(). –

+0

Alors s'il vous plaît vérifier le [docs de Jasmine] (https://jasmine.github.io/2.0/introduction.html#section-Setup_and_Teardown) ;-), mais 'beforeAll()' devrait faire l'affaire si vous êtes sur Jasmine 2.1 – wswebcreation

+0

Merci AvantAll() a travaillé comme un charme! @wswebcreation –