Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog Post - Karate Test Automation #263

Merged
merged 13 commits into from
Nov 1, 2024

Conversation

sdewar-scottlogic
Copy link
Contributor

Please add a direct link to your post here:

https://sdewar-scottlogic.github.io/blog/2024/10/28/an-introduction-into-karate-test-automation.html

Have you (please tick each box to show completion):

  • Added your blog post to a single category?
  • Added a brief summary for your post? Summaries should be roughly two sentences in length and give potential readers a good idea of the contents of your post.
  • Checked that the build passes?
  • Checked your spelling (you can use npm install followed by npx mdspell "**/{FILE_NAME}.md" --en-gb -a -n -x -t if that's your thing)
  • Ensured that your author profile contains a profile image, and a brief description of yourself? (make it more interesting than just your job title!)
  • Optimised any images in your post? They should be less than 100KBytes as a general guide.

Posts are reviewed / approved by your Regional Tech Lead.

Copy link
Contributor

@dogle-scottlogic dogle-scottlogic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff Stu! Couple of thoughts in general,

  • Could do with a scattering of links to some things folks might want to read more about / might not have heard of (Cucumber?)
  • There's a lot of abbreviation which feels a bit odd, there's no character limit (e.g. & and +)
  • Could do with a little more hand holding through the examples personally to make it easier to understand what's happening for the layman

Give me a shout if you want to chat on anything 😄

- API
- Open Source
- Karate
summary: In this blog i'll introduce the Karate Test Automation Framework and talk about some of the fun and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll

offerings in the open source space - don't even ask me how many times I've been bitten by Selenium's reluctance to play
nice with UI elements or been snowed under trying to get my head around a complex multi-util, multi-file test scenario.

Karate is essentially a Gherkin-like programming language, with the ability to use Java & JavaScript almost seamlessly
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth a link to Gherkin for those unfamiliar

Comparison Testing, amongst other features.

# So Stuart, what are you going to show us?
There's a whole host of interesting & handy features of the Framework, but I'll focus on a few that make API testing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ampersand feels unnecessary? Also not sure framework needs a capital

implementing the step-definitions correctly, which means higher time investment and ultimately less readable code in the
background.

Karate lets us use keywords straight out the box, no need for step-definitions & no need for code, everything is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

& again?

And match response.data[*].first_name contains "Tobias"
~~~

That's all we need - this test that will verify that the `users` endpoint returns a 200. Additionally, we run an
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"this test that will verify that the" 😕

of using Karate, I feel that it provides a really nice middle-ground that empowers the tester to make the decision on
how readable their test scenarios are.

## Hybrid Scenarios (+ a sneak peek into Karate UI Automation)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • -> plus?

~~~

Firstly, we have our API call to create the new user. We then need to tell Karate that our `baseUrl` has now changed to
the UI one. Then we instantiate a `driver` instance, and use locators stored in a `homePagelocators.json` file in order
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth linking to the docs a bit for some things like driver for example https://karatelabs.github.io/karate/karate-core/#driver

Lastly, I want to talk about Dynamic Scenario Outlines.

If you're used to Cucumber then you've probably got an understanding of Scenario Outlines - they let you run through
the exact same test steps but with your variables & data driven directly from a table, for example:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

& 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll not comment each one going forward...


This is great for running through a bulk set of tests where we don't necessarily know some vital inputs before
execution. There's a few others ways to do something similar in Karate, but another benefit of Dynamic Scenario Outlines
is that Karate will still respect any parallel execution configuration, meaning we can run this across `x` threads with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think n is probably a more common variable than x here - having x in backticks made me think I'd missed a reference to it somewhere above. Could you replace x with maybe "multiple"?

no extra config outside of the parallel runner configuration.

# Conclusion
The aim of this blog post was just to show some features of the Karate Test Framework that make API (and UI!) testing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conclusion falls a bit flat for me personally - would be good to reword this as a summary of your thoughts on things here.
I'd say something like "look out for future posts where I'll dive deeper into..."
I'd remove the line and resources heading and just make that paragraph part of the conclusion text personally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What Dave said: don't be shy!

Tell us there'll be a follow-up, and that might even encourage you to write it 😉

Copy link
Member

@chriswilty chriswilty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice read 👏

Not much else to add beyond what Dave has already said. I know this is not easy, but try to imagine you are reading those code blocks for the first time: are all the important parts indicated?

# Tell Karate that this is our "base url" for all Scenarios in this Feature File
* baseUrl 'https://reqres.in/api'

@setup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This annotation is used for the first time here, I missed it first time through... Can you call it out in the description?

~~~

Since we told our Scenario Outline to use the `userData` array as its data source via our `Examples` table, it will have access
to any key-value pair present in that array. Here's a snippet of the data array from the `users` endpoint:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An array doesn't have key-value pairs, it's the user entries that have those. Are you saying that we can use the id key which will be replaced with the value? That was a little unclear to me.

is that Karate will still respect any parallel execution configuration, meaning we can run this across `x` threads with
no extra config outside of the parallel runner configuration.

# Conclusion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Level 2 heading here:

## Conclusion

author: sdewar
---

# Introduction
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to level2 heading: ## Introduction

In fact, you arguably don't need "Introduction" as a heading at all, because we can expect some introduction at the top of a blog post.

no extra config outside of the parallel runner configuration.

# Conclusion
The aim of this blog post was just to show some features of the Karate Test Framework that make API (and UI!) testing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What Dave said: don't be shy!

Tell us there'll be a follow-up, and that might even encourage you to write it 😉


[Karate Documentation](https://karatelabs.github.io/karate/)

[Stack Overflow](https://stackoverflow.com/questions/tagged/karate)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link text should be more descriptive, e.g. "Karate on Stack Overflow"

Copy link
Contributor

@dogle-scottlogic dogle-scottlogic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loads better 👍 I was able to follow the examples a lot easier this time round. Minor one but I can't access the deployed page as the time is set to 12:00:00 Z


Karate lets us write our test scenarios straight out the box - there's no need to write Java glue code or
step-definitions like Cucumber. The example below showcases this perfectly - there's no additional setup needed
or extra digging into the technical behind-the-scenes implementation in order to get testing our services.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the technical, behind-the-scenes implementation

chain commands on the back of the locator being found as opposed to waiting for the locator, storing it, then performing
an action.

This means we have a tidy of steps that will spin up our driver, wait for elements to appear, perform
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"This means we have a tidy of steps"?

ID's and then send a DELETE request to `https://reqres.in/api/users/${id}` for each one.

In the below example, we have a `@setup` Scenario which is our data setup for our Scenario Outline.
Up until now we are used to the `Background` section running for every Scenario, but that's not the case here, so we need
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might just be me - why is it not the case? Is it because @setup causes that to behave differently? If so why do we need to define the baseUrl in background at all here?

Copy link
Contributor Author

@sdewar-scottlogic sdewar-scottlogic Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah its a weird quirk of the implementation that tripped me up for many hours, @setup runs before background so @setup doesn't know about anything we've set up in background.

We still need it in the background so our Scenario Outline (and any other scenarios in the file) know what the baseUrl is. I'll update and perhaps link to that specific part of the docs here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok, at least it's not just me being dumb 😆

a deeper dive into some of the other key features such as exploring the comprehensive assertion features, image
comparison, built in HTML reporting or data driven tests via tags.

The Documentation for Karate is really solid, with lots of examples and good explanations of each bit of functionality.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

capital D not needed?

Copy link
Contributor

@dogle-scottlogic dogle-scottlogic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 🚀

Copy link
Member

@chriswilty chriswilty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👌

@chriswilty chriswilty merged commit 0c4f622 into ScottLogic:gh-pages Nov 1, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants