Getting the spec name in Protractor for Sauce Labs

Say that you’re writing tests in Protractor, and say also that you’re running your tests on Sauce Labs.  You want to set your test name in Sauce Labs to match the spec.

You normally set the test name in Sauce Labs by sending “name” in the desired capabilities.

capabilities.name = "your test name"

But in Protractor, the browser is created before starting the test.  The order of execution is:

1) Setup environment
2) Create a browser and setup globals 
3) Setup plugins 
4) Execute test cases 
5) Wait for postTest plugins to finish 
6) Teardown plugins 
7) Teardown

(in runner.ts)

The configuration file(s) are read and capabilities set in step 1.  Then the browser is created.  And only in step 4 are the spec files read.

There is an afterEach() hook that allows you to restart the browser for each test (if your test runner supports it, e.g. jasmine, mocha), and when using Sauce Labs (or Browserstack) to update the test status:

this.sauceServer_.updateJob(session.getId(), update, (err: Error) => {

(in sauce.ts)

We could use this updateJob method to also set the test name before each spec, but that would require changing the source code — it’s not available to the conf file or before each.

However, this same method can be called on the client side by using the Javascript executor.  So, in the beforeEach() method you can call:

browser.executeScript("sauce:job-name=whatever you want");

and it will change the test name in Sauce Labs. There is only one thing left to do to make this functional, get the spec name to set in the Javascript Executor.

This can be accomplished by getting the spec as a return value to the spec definition:

var spec = it('should set the spec name in Sauce Labs', function() { 
    browser.executeScript("sauce:job-name=" + spec.getFullName()); 
}

Now your tests in Sauce Labs reflect the text in your carefully worded spec description.

There’s one thing that can make this better.  Instead of adding this step to each spec, set it in your protractor conf.js file using the onPrepare() hook.

If you’re using mocha, no problem, but if you’re using Jasmine (the default Protractor test runner), well, this is a problem:

https://github.com/jasmine/jasmine/issues/611

Let me just say here that often open source project maintainers pull the “because it is better” argument to mean “because I don’t know how”.  Ask Dostoyevsky about the mindset.

But, you could also use a custom reporter in Jasmine

onPrepare: function() {
    jasmine.getEnv().addReporter({
        specStarted: function(result) { 
            browser.executeScript("sauce:job-name=" + result.fullName);
        }
    });
} }

If you add this to your protractor conf.js your tests will update with the spec fullname in Sauce Labs.

Here’s a working example:

https://github.com/sauceaaron/protractor-saucelabs-testname-per-spec

Advertisements

Mapping Maps With Method References

I just learned about “method reference” notation in Java 8 thanks to Intellij IDEA code hints.

Consider the following code:

public class MapMap
{
   Map<String, String> map;
   
   public static void map(Object key, Object value)
   {
      System.out.println(key + ":" + value);
   }
   
   public void mapmap()
   { //...see implementations below }
}

I knew this:

/* loop */ 
for (Map.Entry entry: map.entrySet()) 
{ 
  map(entry.getKey(), entry.getValue()); 
}

could be replaced with this:

/* lambda expression */ 
map.forEach((key, value) -> map(key, value);

but I did not know about this:

/* method reference */ 
map.forEach(MapMap::map);

See the full gist here

 

See here for the official documentation on method references:

https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

 

RESTful Web Services with Powershell

Working with a client recently and needed to check connectivity to a web service.  I wanted to make a few HTTP requests. Normally I’d fire up curl and paste a URL with basic auth credentials:

curl -u $USERNAME:$PASSWORD -k https://api.example.com/some/endpoint

But… they were on Windows. No problem, fire up chocolately and install curl

choco install curl

Ah… this server is locked down and no way they’re going to fill out the paperwork (in triplicate) to install .  Someone said that PowerShell has curl installed.  But wait a minute…

Screen Shot 2017-08-18 at 3.21.40 PM

Someone has played a nasty trick on me.

I can’t use curl, but PowerShell has something not quite, but almost entirely unlike curl.  Let’s see what we can do with Invoke-WebRequest.

Ok, so you can call a URL with:

Invoke-WebRequest -Uri https://api.example.com/some/endpoint

It turns out that passing Basic Authentication credentials is trickier. But wait! stackoverflow comes to the rescue(-ish).

You have a -Credential flag, but all that does is open up a Windows dialog to enter them.  Not very automatable. (automatible? odd-tomato-bull?)

Basic authentication is really just an HTTP header:

GET /some/endpoint HTTP/1.1
Authorization: Basic SSBsb3ZlIEtlbHNleSBGb3g=

And the credentials are just a base64 encoded string containing text in the format

username:password

One last thing though, you need to escape the colon in your credentials.  Apparently backtick is the escape character in Powershell:

$credentials = "$username`:$password"

And you can pass an array of headers with Invoke-WebRequest.   (backtick also allows continuation on the next line)

Invoke-WebRequest `
-Uri https://api.example.com/some/endpoint `
-Headers @{ Authorization = "Basic $encodedCredentials" }

So now I’m getting somewhere.

The only step left is to figure out how to Base64Encode with PowerShell.  I’m not sure where I found it, but this will do the trick:

$encodedCredentials = `
[System.Convert]::ToBase64String( `
[System.Text.Encoding]::ASCII.GetBytes($credentials))

Remember folks, only you can prevent github from sharing your credentials.  Use an environment variable, and don’t put your password in powershell your script:

Here’s how you get an environment variable in Powershell

Get-ChildItem Env:USERNAME

Finally, here is my full script:

$username = $(Get-ChildItem Env:USERNAME).value
$password = $(Get-ChildItem Env:PASSWORD).value

$credentials = "$username`:$password"
$encodedCredentials = `
[System.Convert]::ToBase64String( `
[System.Text.Encoding]::ASCII.GetBytes($credentials))

Invoke-WebRequest `
-Uri https://api.example.com/some/endpoint `
-Headers @{ Authorization = "Basic $encodedCredentials" }

But wait, there’s more!

Now that I’ve got my response I can actually use Powershell to parse the JSON response and access it using ConvertFrom-Json.:

Given the following JSON

[{ "contact": { "name": "Aaron Evans", "website": { "url": "http://one-shore.com" }, "blog": { "url": "https://fijiaaron.wordpress.com" } }}]

I could parse it like this:

$response = $(Invoke-WebRequest -Uri "$endpoint" -Headers $headers)
$contacts = $(ConvertFrom-Json -InputObject $response.Content)

Invoke-WebRequest -Uri  $contacts[0].contact.blog.url

Please don’t flood my blog with hits.  I haven’t monetized it yet.

See the whole thing with this gist:

 

 

 

Disable Integrated Windows Authentication (IWA) for Selenium

Here’s the problem:

You have a site that you’d like to write automated tests for.  But when you attempt to login with Internet Explorer, it has a Windows Authentication dialog popup. Because this is a native UI element, Selenium can’t touch it.

If you have control of the machine (and the Windows ADFS domain controller), you can add the user and the popup will go away.  But if you don’t have control — like if you’re running your tests on a virtual machine in the cloud with Sauce Labs — you need to find a way to get around it.

I recently ran into this problem.  It only affected Internet Explorer.  Chrome and Firefox were directed to a web login form — which could be automated with Selenium.  The options were to either A) figure out how to authenticate with Windows, or B) bypass Windows Authentication so it doesn’t have the native popup, and it doesn’t block tests.

I think I’ve found a solution.

You can disable Integrated Windows Authentication under “Internet Options” for Internet Explorer.  Under the “Advanced” tab, scroll down to “Security” and uncheck “Enable Integrated Windows Authentication”.  That should do it.

 

 

But there was still the task of automating this step.  I thought of trying a macro tool like AutoIt, but that just didn’t feel right.  Plus, it can be tricky to actually use AutoIt.  I don’t want to learn their language, and the recorder has been removed from AutoIt.

Click here to see how to get an older version of AutoIt that still has a working recorder:

https://www.autoitscript.com/forum/topic/176009-where-is-au3recordexe/

I knew there must be some way to do this without resorting to UI automation.  So, after a little poking around, I found this old blog post:

https://laxmanchip.wordpress.com/2011/12/16/enabledisable-integrated-windows-authentication-vbscript/

Thanks Tom!

A little more searching taught me all I need to know about using Powershell to edit registry keys:

https://blogs.technet.microsoft.com/heyscriptingguy/2015/04/02/update-or-add-registry-key-value-with-powershell/

Combining the two gave me this nice little script:

Set-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -name EnableNegotiate -value 0
Get-Item "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

 

And now I can execute that on a remote computer before running my Selenium automation!

Sauce Labs has the following in their wiki about using AutoIt as a pre-run executable:

https://wiki.saucelabs.com/display/DOCS/Running+an+AutoIt+Script+as+a+Pre-run+Executable+to+Handle+Windows+Security+Authentication+Dialogs

And some general information about specifying a pre-run executable in your Desired Capabilities.

Thanks Sauce Labs!

And thanks to the team that set up this challenge — you know who you are ;)

 

 

Weekly Wednesday Webinar on Selenium & Sauce Labs

I’ve been working at Sauce Labs for a while now, helping enterprise users build test automation frameworks and implement continuous integration using Selenium & Sauce Labs.

In order to reach a larger audience — and to learn more about people’s challenges developing test automation — I’m going to be hosting a weekly webinar on using Selenium with Sauce Labs for test automation.

So, starting this week, each Wednesday during lunch (12:30pm Mountain Time) I’ll host a webinar / office hours.  I’ll begin with a brief presentation introducing the topic, followed by a demo (live coding — what could go wrong?), and then open it up for questions & comments.

The first webinar will be tomorrow at 12:30pm MST.  The topic is DesiredCapabilities.

I’ll talk about what desired capabilities are, how to use desired capabilities with Sauce Labs, and show how you can use the  Sauce Labs platform configurator to generate desired capabilities.  I’ll also talk about Sauce Labs specific capabilities used to report on tests and builds.

Register on EventBrite here: Selenium & Sauce Labs Webinar: Desired Capabilities

Join the WebEx directly: Selenium & Sauce Labs Webinar: Desired Capabilities

Contact me if you’d like a calendar invite or more info.

Testing PDF downloads with Sauce Labs

There are 2 main challenges to testing PDF downloads with Sauce Labs:

1. Testing PDFs in the browser are not supported by Selenium.
2. Download dialogs are native UI elements and not supported by Selenium.

A PDF file may try to open in a PDF plugin, which cannot be accessed by Selenium. But, even if you change your browser configuration to download PDF files automatically, as described here:

http://elementalselenium.com/tips/2-download-a-file

Sauce Labs does not provide access to the file system on our VMs. So, in order to test a PDF you would need to download it locally. The best practice is to get the URL of the link using Selenium and then to download it using wget, curl, or your favorite HTTP library.

See this blog post which goes into further detail:

http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/