Salesforce, Python, SQL, & other ways to put your data where you need it

Need event music? 🎸

Live and recorded jazz, pop, and meditative music for your virtual conference / Zoom wedding / yoga class / private party with quality sound and a smooth technical experience

Check a URL with Selenium IDE

06 Apr 2022 🔖 selenium intermediate tutorials web development
💬 EN

Table of Contents

It took me a while to figure out how to get Selenium IDE to click a link, wait for the new tab’s page-load to settle down, and validate the URL of the new page, but I did it.

Overall, I’m pretty satisfied with Selenium IDE. If you have business processes that involve visiting a web site and clicking around a bunch to “make sure everything looks good,” it’s a nifty tool for pretending to be yourself, only sped up really really fast.

Steps

The one and only test present in this Selenium IDE project runs through 8 steps:

  1. Open the Katie Kodes donations page on Ko-Fi
    • Command: open
    • Target: katiekodes (on top of a test base URL of https://ko-fi.com/)
      Support me on Ko-Fi
  2. Click the Katie Kodes blog link
    • Command: click
    • Modify new window configuration -> New Window Configuration Enabled: checked, Window Name: blog_new_tab, Timeout: 2000, click “Confirm”
    • Target: xpath=(//a[@target="_blank" and contains(@href, "katiekodes.com")])[1]
  3. Close the original Ko-Fi tab
    • Command: close
  4. Make sure we are viewing the new blog tab
    • Command: select window
    • Target: handle=${blog_new_tab}
  5. Wait for something bloggey to be on the screen
    • Command: wait for element present
    • Target: xpath=(//a[contains(@href, "ko-fi.com")])[1]
    • Value: 30000
    • (Technically, this isn’t required for this example, but the situation I was dealing with opened a new tab and then handed off cookies through about 4 different URLs before settling down on one, so I decided to validate that things were “all done” by looking for a weird, distinctive HTML element I knew only existed in the final, settled page.)
  6. Save the blog URL to a variable
    • Command: execute script
    • Target: return document.URL;
    • Value: theurl
  7. Assert that the blog URL is correct
    • Command: assert
    • Target: theurl
    • Value: https://katiekodes.com/
  8. Close the web browser
    • Command: close

Code

To try it yourself, save this code as Selenium Demo.side on your computer and open it as a project with the Selenium IDE browser plugin, then click the “Validate the new-tab URL” test and click “Run current test.”

{
  "id": "ca2c89aa-50b1-4eea-91e6-c2e01ae6cb0a",
  "version": "2.0",
  "name": "Selenium demo",
  "url": "https://ko-fi.com/",
  "tests": [{
    "id": "e363b287-2838-40db-b1b8-57dd100af3a5",
    "name": "Validate the new-tab URL",
    "commands": [{
      "id": "712005e0-c6ec-4f10-9162-da97499d5e23",
      "comment": "Open Katie Kodes Ko-Fi donations page",
      "command": "open",
      "target": "katiekodes",
      "targets": [],
      "value": ""
    }, {
      "id": "9eaf4f01-f179-4c19-855c-4eb9a1968158",
      "comment": "Click the Katie Kodes blog link",
      "command": "click",
      "target": "xpath=(//a[@target=\"_blank\" and contains(@href, \"katiekodes.com\")])[1]",
      "targets": [],
      "value": "",
      "opensWindow": true,
      "windowHandleName": "blog_new_tab",
      "windowTimeout": 2000
    }, {
      "id": "3c0233e1-b2d4-4c82-b806-7d9133e94e96",
      "comment": "Close the original Ko-Fi tab",
      "command": "close",
      "target": "",
      "targets": [],
      "value": ""
    }, {
      "id": "5683db68-8b78-48a7-b959-84ebccd70119",
      "comment": "Make sure we are viewing the new blog tab",
      "command": "selectWindow",
      "target": "handle=${blog_new_tab}",
      "targets": [],
      "value": ""
    }, {
      "id": "a37060a8-8e9b-4848-9068-f4a1fcb5f2ca",
      "comment": "Wait for something bloggey to be on the screen",
      "command": "waitForElementPresent",
      "target": "xpath=(//a[contains(@href, \"ko-fi.com\")])[1]",
      "targets": [],
      "value": "30000"
    }, {
      "id": "ff897cc8-d4f0-4762-8695-d9551d7f4e96",
      "comment": "Save the blog URL to a variable",
      "command": "executeScript",
      "target": "return document.URL;",
      "targets": [],
      "value": "theurl"
    }, {
      "id": "b3b8f50b-4266-4c68-902b-33e150279274",
      "comment": "Assert that the blog URL is correct",
      "command": "assert",
      "target": "theurl",
      "targets": [],
      "value": "https://katiekodes.com/"
    }, {
      "id": "68c8b88e-84bb-4c80-b914-3a0278995967",
      "comment": "Close the web browser",
      "command": "close",
      "target": "",
      "targets": [],
      "value": ""
    }]
  }],
  "suites": [{
    "id": "398fd234-8bcf-48ff-ab6c-4785b1ab469f",
    "name": "My first suite",
    "persistSession": false,
    "parallel": false,
    "timeout": 300,
    "tests": ["e363b287-2838-40db-b1b8-57dd100af3a5"]
  }],
  "urls": ["https://ko-fi.com/"],
  "plugins": []
}

Selenium IDE caveats

Test design

It helps a lot to understand both “how the web site you’re testing supposed to work” and a good deal of HTML.

Right-clicking on, say, a link in Selenium IDE’s recorder and telling it that you want to verify that it’s labeled “Hello World” is kind of useless when the Selenium IDE recorder decides that it should select the link by … looking for the link that says “Hello World.”

(Meaning if the test fails, it fails because it can’t find the link, not because it says the wrong thing. D’oh.)

So being able to do a little surgery in Selenium IDE to improve the “selector” for the HTML element you’re interested in is useful.

It also helps not to be completely overwhelmed by small bits of JavaScript, because things like “Validate the contents of the URL I’m now on” doesn’t come with Selenium IDE’s commands – instead you have to write 3 words of JavaScript code to get the URL of whatever page Selenium IDE is visiting on your behalf.

Team use

Even if not every tester on your team both understands the product and understands HTML, the tool is free and maintained by the official Selenium project, so maybe just one person could design the .side file and then share it around with everyone else.

Even if some tests fail because every tester’s experience of a site might be a little different, as long as they understand that and you’ve labeled the tests & their steps well enough for them to be able to troubleshoot, “Oh, that makes sense that that failed for me,” it should still be better than nothing.

Suites of tests run all the tests even if one test fails, so it’s not like things are going to “get stuck” if one or two tests fail.

Test execution

Selenium IDE, at least as run as a Chrome plugin, just piggybacks on your current Chrome state, including cookies of sites you’re logged into, and opens new tabs for each test it runs.

So it works well for testing sites that you have to be logged into, as long as you leave a browser window open in the background that you’ve already logged into the site.

If half the battle is getting logged in, Selenium IDE won’t make your life much better. But if, once you’re in, you have dozens or hundreds of parts of the site to click around, you might really like Selenium IDE.

Security

Speaking of testing web sites behind login-walls…

Software is only as secure as the good intentions of the people who publish it.

While these browser plugins are maintained by a well-respected open-source project, even open-source projects are only as good as the careful review of the gatekeepers accepting “volunteer contributions.”

If you install any plugin into a web browser, it’s pretty much now you on the web. As long as you install a version that’s in good shape, it won’t do anything weird like “phone home” exfiltrating data like your passwords … but … y’know, if you accidentally install a hacked update … it could. So give it its due skepticism at work and maybe have a colleague who can actually understand the browser plugin’s codebase do a quick look-over to make sure the plugin still isn’t doing anything weird when you get ready to install it.

Further reading

--- ---