Skip to main content

Posts

Showing posts with the label protractorjs

Mock HTTP calls with Angular and Protractor

One of the benefits using AngularJS is also using some test tools like Protractor. Protractor is a tool that was made for Angular testing. Many times when testing you will need to mock some data so that you can see if your application responds in the correct manner. Let us assume that we have the following HTML file running our application. <!doctype html> <html ng-app="demo"> <head> <title>Hello</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-mocks.js"></script> <script src="hello.js"></script> </head> <body> <div ng-controller="Hello"> <p>Hello <span id="gname">{{exampleResponse.name}}</span></p> </div> ...

Protractor getText from hidden element.

Ran across a point that I wanted to get the value of a hidden element while running my Protractor tests. getText() function was not helpful at all. In order for it to return text it has to be visible. There is an option called getAttribute('textContent'). This will allow you to always return the text. Sample HTML <span style="display: none" name="hiddenSpan">Hidden Text</span> Sample Protractor JS $('[name="hiddenSpan"]').getText(); // returns "" $('[name="hiddenSpan"]').getAttribute('textContent'); // returns "Hidden Text"

Query Selector: How to Or Selection

This is probably an odd topic, however I was recently asked how could one select between 2 possible elements on a browser using Protractor. If you are unfamiliar with Protractor it is a testing tool for AngularJS applications. To start lets show a code example of what we are looking at. <html> <body> <div id='ex1'> <!-- some code here --> </div> <div id='ex2'> <!-- some code here --> </div> </body> </html> Notice from the code that we have 2 divs with id's ex1 and ex2. Now depending on the code you ex1 or ex2 can be hidden. Lets say hypothetically they both do the exact same thing and you just want to click which ever one happens to be available. At the same time you also don't want to add expensive checks for isPresent or waits. To get around this you can do the following with the by.css query selector. $('[id="ex1"], [id="ex2"]') ...

Scrolling to place on webpage for Protractor or Webdriver testing.

Sometimes when using a Selenium by product like Protractor for testing Angular applications, I run into issues trying to access certain elements. In some cases it is because our UI folks may have another element that hovers over an area rather than actually take up that space. In cases like these I need to move the content on the page similar to how a user would so that the content I need is the front most element on the page. To do this I made a simple scrollTo function that will take a Selenium element and tell the browser with executeScript to goto those coordinates. I also have an offset so that I can move it around the would be elements that cover my element. function scrollToElement(element, offset) { offset = offset || 0; element.getLocation() .then( function(loc) { browser.executeScript(('window.scrollTo('+loc.x+','+ (loc.y - offset) +');')) }); }