-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselenium-ruby.rb
51 lines (49 loc) · 2.48 KB
/
selenium-ruby.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'rubygems'
require 'selenium-webdriver'
options = Selenium::WebDriver::Options.chrome
options.browser_version = 'latest'
options.platform_name = 'MAC'
bstack_options = {
"os" => "OS X",
"osVersion" => "Sierra",
"buildName" => "Final-Snippet-Test",
"sessionName" => "Selenium-4 Ruby snippet test",
"local" => "false",
"seleniumVersion" => "4.0.0",
}
options.add_option('bstack:options', bstack_options)
driver = Selenium::WebDriver.for(:remote,
:url => "https://YOUR_USER_NAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
:capabilities => options)
begin
# opening the bstackdemo.com website
driver.navigate.to "https://bstackdemo.com"
wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
wait.until { !driver.title.match(/StackDemo/i).nil? }
# getting name of the product available on the webpage
product = driver.find_element(:xpath, '//*[@id="1"]/p')
wait.until { product.displayed? }
product_text = product.text
# waiting until the Add to Cart button is displayed on webpage and then clicking it
cart_btn = driver.find_element(:xpath, '//*[@id="1"]/div[4]')
wait.until { cart_btn.displayed? }
cart_btn.click
# waiting until the Cart pane appears
wait.until { driver.find_element(:xpath, '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]').displayed? }
# getting name of the product in the cart
product_in_cart = driver.find_element(:xpath, '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]')
wait.until { product_in_cart.displayed? }
product_in_cart_text = product_in_cart.text
# checking if the product has been added to the cart
if product_text.eql? product_in_cart_text
# marking test as 'passed' if the product is successfully added to the cart
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Product has been successfully added to the cart!"}}')
else
# marking test as 'failed' if the product is not added to the cart
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Failed to add product to the cart"}}')
end
# marking test as 'failed' if test script is unable to open the bstackdemo.com website
rescue
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some elements failed to load"}}')
end
driver.quit