Using events to trigger Selenium based actions for website testing
Exercise: Using Selenium to add items to a shopping cart
Open your terminal and navigate to a directory where you have write permissions. Execute the following command and respond to the prompts:
$ provengo create EX-selenium
Executing the command will generate a directory named EX-selenium
, which contains a hello_world.js
file located in the spec/js
subdirectory. Clear the existing content of this file and replace it with the following code. If desired, you can also rename the file:
// @provengo summon selenium
const URL = 'https://demo.prestashop.com/#/en/login'
const SESSION = "customer-session"
user = {
email: 'bob.smith@prestashop.com',
password: 'bob.smith@prestashop.com'
}
product = {
name: 'HUMMINGBIRD PRINTED T-SHIRT'
}
bthread('login', function () {
with (new SeleniumSession(SESSION).start(URL)) {
sync({ request: Event("Begin(login)") })
switchFrame('//iframe[contains(@id,"framelive")]')
waitForVisibility('//img[contains(@src,"logo")]', 50000)
click('//span[contains(.,"Sign in")]')
writeText('//input[@id="field-email"]', user.email)
writeText('//input[@id="field-password"]', user.password)
click('//button[@id="submit-login"]')
sync({ request: Event("End(login)") })
}
})
bthread("add to cart", function () {
with (new SeleniumSession(SESSION).start(URL)) {
sync({ request: Event("Begin(addToCart)") })
writeText('//input[@name="s"]', product.name + '\n')
click('(//div[@id="js-product-list"]//a)[1]')
click('//button[@data-button-action="add-to-cart"]')
click('//div[h4[contains(text(),"Product successfully added to your shopping cart")]]/button')
sync({ request: Event("End(addToCart)") })
}
})
bthread('Add do card cannot start before login ends to cart', function () {
// Your code goes here
})
Your task is to introduce a third bthread that enforces that addToCart
does not begin befor login
ends.
Without your code, the command:
$ provengo analyze -f pdf EX-selenium
will generate a file named EX-selenium/products/run-source/testSpace.pdf
. That looks like this:
It represents the fact that the two bthreads can run in parallel. Your task is to introduce a third bthread that enforces that addToCart
does not begin befor login
ends. When you are done, the file should look like this:
If you get a different result, try to figure out what went wrong. If you get stuck, you can find the solution in the EX-selenium/solution
directory.