import os
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import behave_webdriver
from behave_webdriver.steps import when, then, step
@step('I take a screenshot and name it "{filename}"')
[docs]
def take_screenshot(context, filename):
pic_path = os.path.join(context.pictures_dir, filename+'.png')
context.behave_driver.get_screenshot_as_file(pic_path)
if context._config.format[0] == 'allure':
import allure
with open(pic_path, 'rb') as png_file:
png_bytes = png_file.read()
allure.attach(png_bytes, name="img", attachment_type=allure.attachment_type.PNG)
@step('I use a {browser} browser')
[docs]
def launch_driver(context, browser):
if browser == 'Chrome':
executable_path = '/usr/bin/chromedriver'
else:
raise AssertionError(f"Unsupported browser: {browser}")
context.behave_driver = getattr(behave_webdriver, browser)(executable_path=executable_path)
context.behave_driver.implicitly_wait(3)
context.behave_driver.maximize_window()
@when('I log in the Quandela cloud')
[docs]
def log_in_cloud(context):
assert context.behave_driver.current_url == "https://cloud.quandela.dev/webide/login"
email_input = context.behave_driver.find_element_by_xpath(
'/html/body/div[1]/div/div[2]/div[2]/form/div[1]/div/div[1]/div/input')
email_input.send_keys(context.users[0]['email'])
password_input = context.behave_driver.find_element_by_xpath(
'/html/body/div[1]/div/div[2]/div[2]/form/div[2]/div/div[1]/div/input')
password_input.send_keys(context.users[0]['password'])
login_button = context.behave_driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[2]/form/button')
login_button.click()
@when('I wait for the url "{url}" to load')
[docs]
def wait_for_url_to_be_loaded(context, url):
WebDriverWait(context.behave_driver, 3).until(EC.url_to_be(url))
@when('I click on the element with text "{text}"')
[docs]
def click_on_element_with_text(context, text: str):
element = context.behave_driver.find_element_by_partial_link_text(text)
element.click()
def _find_element_that_contain_text(context, tag: str, text: str):
return context.behave_driver.find_element_by_xpath(f'//{tag}[contains(text(), "{text}")]')
@then('I expect that an element with "{tag}" tag and text "{text}" exists')
[docs]
def element_with_test_exists(context, tag: str, text: str):
if not tag or tag == 'any':
tag = '*'
try:
_find_element_that_contain_text(context, tag, text)
except NoSuchElementException:
assert False, f'Element with "{tag}" tag and text "{text}" does not exist'