Best Python code snippet using selene_python
web_ui_controller.py
Source:web_ui_controller.py
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3import pysnooper4from time import sleep5from selenium import webdriver6from selenium.common.exceptions import NoSuchElementException7from selenium.webdriver import ActionChains8@pysnooper.snoop()9class WebUiBaseLib:10 """11 selenium test operation library12 """13 def __init__(self, url, down_path='.'):14 options = webdriver.ChromeOptions()15 prefs = {16 'profile.default_content_settings.popups': 0,17 'download.default_directory': down_path18 }19 options.add_experimental_option('prefs', prefs)20 options.add_argument("blink-settings=imagesEnabled=false")21 options.add_argument("--headless")22 options.add_argument("--disable-gpu")23 options.add_argument("--no-sandbox")24 options.add_argument("--disable-dev-shm-usage")25 options.add_argument("--disable-extensions")26 self.driver = webdriver.Chrome(chrome_options=options)27 self.driver.get(url)28 def click(self, element_by, element):29 """30 Click the button31 :param element_by:Element type32 :param element:Element33 :return:Boolean value34 """35 if not self.wait_element(element_by, element):36 return False37 flag = None38 try:39 if element_by == "id":40 self.driver.find_element_by_id(element).click()41 elif element_by == "xpath":42 self.driver.find_element_by_xpath(element).click()43 elif element_by == "class":44 self.driver.find_element_by_class_name(element).click()45 elif element_by == "link_text":46 self.driver.find_element_by_link_text(element).click()47 elif element_by == "partial_link_text":48 self.driver.find_element_by_partial_link_text(element).click()49 elif element_by == "name":50 self.driver.find_element_by_name(element).click()51 elif element_by == "tag":52 self.driver.find_element_by_tag_name(element).click()53 elif element_by == "css":54 self.driver.find_element_by_css_selector(element).click()55 flag = True56 sleep(0.5)57 except NoSuchElementException:58 flag = False59 return flag60 def context_click(self, element_by, element):61 """62 Click the button63 :param element_by:Element type64 :param element:Element65 :return:Boolean value66 """67 if not self.wait_element(element_by, element):68 return False69 flag = None70 try:71 if element_by == "id":72 element_obj = self.driver.find_element_by_id(element)73 ActionChains(self.driver).context_click(element_obj).click().perform()74 elif element_by == "xpath":75 element_obj = self.driver.find_element_by_xpath(element)76 ActionChains(self.driver).context_click(element_obj).click().perform()77 elif element_by == "class":78 element_obj = self.driver.find_element_by_class_name(element)79 ActionChains(self.driver).context_click(element_obj).click().perform()80 elif element_by == "link_text":81 element_obj = self.driver.find_element_by_link_text(element)82 ActionChains(self.driver).context_click(element_obj).click().perform()83 elif element_by == "partial_link_text":84 element_obj = self.driver.find_element_by_partial_link_text(element)85 ActionChains(self.driver).context_click(element_obj).click().perform()86 elif element_by == "name":87 element_obj = self.driver.find_element_by_name(element)88 ActionChains(self.driver).context_click(element_obj).click().perform()89 elif element_by == "tag":90 element_obj = self.driver.find_element_by_tag_name(element)91 ActionChains(self.driver).context_click(element_obj).click().perform()92 elif element_by == "css":93 element_obj = self.driver.find_element_by_css_selector(element)94 ActionChains(self.driver).context_click(element_obj).click().perform()95 flag = True96 sleep(0.5)97 except NoSuchElementException:98 flag = False99 return flag100 def get_element_text(self, element_by, element):101 """102 Get component attribute103 :param element_by:Element type104 :param element:Element105 :param attribute:Get attributes106 :return:Get information107 """108 if not self.wait_element(element_by, element):109 return False110 flag = None111 try:112 if element_by == "id":113 flag = self.driver.find_element_by_id(element).text114 elif element_by == "xpath":115 flag = self.driver.find_element_by_xpath(element).text116 elif element_by == "class":117 flag = self.driver.find_element_by_class_name(element).text118 elif element_by == "link_text":119 flag = self.driver.find_element_by_link_text(element).text120 elif element_by == "partial_link_text":121 flag = self.driver.find_element_by_partial_link_text(122 element).text123 elif element_by == "name":124 flag = self.driver.find_element_by_name(element).text125 elif element_by == "tag":126 flag = self.driver.find_element_by_tag_name(element).text127 elif element_by == "css":128 flag = self.driver.find_element_by_css_selector(element).text129 except NoSuchElementException:130 flag = False131 return flag132 def send_element_content(self, element_by, element, content):133 """134 Text into the specified element135 :param element_by:Element type136 :param element:Element137 :param content:Text content138 :return:Boolean value139 """140 if not self.wait_element(element_by, element):141 return False142 flag = None143 try:144 if element_by == "id":145 self.driver.find_element_by_id(element).send_keys(content)146 elif element_by == "xpath":147 self.driver.find_element_by_xpath(element).send_keys(content)148 elif element_by == "class":149 self.driver.find_element_by_class_name(element).send_keys(150 content)151 elif element_by == "link_text":152 self.driver.find_element_by_link_text(element).send_keys(153 content)154 elif element_by == "partial_link_text":155 self.driver.find_element_by_partial_link_text(156 element).send_keys(content)157 elif element_by == "name":158 self.driver.find_element_by_name(element).send_keys(content)159 elif element_by == "tag":160 self.driver.find_element_by_tag_name(element).send_keys(161 content)162 elif element_by == "css":163 self.driver.find_element_by_css_selector(element).send_keys(164 content)165 flag = True166 except NoSuchElementException:167 flag = False168 return flag169 def is_element(self, identify_by, element):170 """171 Determine whether elements exist172 :param identify_by:Element type173 :param element:Element174 :return: Boolean value175 """176 flag = None177 try:178 if identify_by == "id":179 self.driver.find_element_by_id(element)180 elif identify_by == "xpath":181 self.driver.find_element_by_xpath(element)182 elif identify_by == "class":183 self.driver.find_element_by_class_name(element)184 elif identify_by == "link_text":185 self.driver.find_element_by_link_text(element)186 elif identify_by == "partial_link_text":187 self.driver.find_element_by_partial_link_text(element)188 elif identify_by == "name":189 self.driver.find_element_by_name(element)190 elif identify_by == "tag":191 self.driver.find_element_by_tag_name(element)192 elif identify_by == "css":193 self.driver.find_element_by_css_selector(element)194 flag = True195 except NoSuchElementException:196 flag = False197 return flag198 def wait_element(self, element_by, element, time=60):199 """200 Wait for element to appear201 :param element_by:Element type202 :param element:Element name203 :param time: Actual waiting time = time * 10204 :return:Boolean value205 """206 for order in range(int(time)):207 sleep(1)208 if self.is_element(element_by, element):209 return True210 return False211 def quit_driver(self):212 """213 quit driver214 @return:215 """...
app_ui_controller.py
Source:app_ui_controller.py
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3from appium import webdriver4from selenium.webdriver.support.ui import WebDriverWait5from selenium.webdriver.support import expected_conditions6class AppUiBaseLib:7 def __init__(self, appium_port, **capabilities):8 remote_server_url = "http://127.0.0.1:" + str(appium_port) + "/wd/hub"9 self.driver = webdriver.Remote(remote_server_url, capabilities)10 def quit_driver(self):11 self.driver.quit()12 def find_element(self, element_by, element):13 if not self.is_element_present(element_by, element):14 return False15 if element_by == "id":16 element_obj = self.driver.find_element_by_id(element)17 elif element_by == "xpath":18 element_obj = self.driver.find_element_by_xpath(element)19 elif element_by == "class":20 element_obj = self.driver.find_element_by_class_name(element)21 elif element_by == "link_text":22 element_obj = self.driver.find_element_by_link_text(element)23 elif element_by == "partial_link_text":24 element_obj = self.driver.find_element_by_partial_link_text(element)25 elif element_by == "name":26 element_obj = self.driver.find_element_by_name(element)27 elif element_by == "tag_name":28 element_obj = self.driver.find_element_by_tag_name(element)29 elif element_by == "css_selector":30 element_obj = self.driver.find_element_by_css_selector(element)31 elif element_by == "accessibility_id":32 element_obj = self.driver.find_element_by_accessibility_id(element)33 elif element_by == "uiautomator":34 element_obj = self.driver.find_element_by_android_uiautomator(element)35 else:36 element_obj = False37 return element_obj38 def click_element(self, element_by, element):39 element_obj = self.find_element(element_by, element)40 if element_obj:41 element_obj.click()42 return True43 return False44 def get_element_attribute(self, element_by, element, attribute="text"):45 element_obj = self.find_element(element_by, element)46 if element_obj:47 return element_obj.get_attribute(attribute)48 return False49 def send_keys(self, element_by, element, command):50 element_obj = self.find_element(element_by, element)51 if element_obj:52 element_obj.send_keys(command)53 return True54 return False55 def is_element_present(self, element_by, element):56 try:57 self.driver.find_element(element_by, element)58 except Exception as e:59 print(e)60 return False61 else:62 return True63 def swipe(self):64 window_size = self.driver.get_window_size()65 width = window_size['width'] * 0.566 original_height = window_size['height'] * 0.567 target_height = window_size['height'] * 0.2568 self.driver.swipe(width, original_height, width, target_height, 500)69 def wait_element(self, element_by, element, timeout=10):70 msg = "Not Fond:" + element71 try:72 WebDriverWait(self.driver, timeout).\73 until(expected_conditions.presence_of_element_located((element_by, element)), msg)74 except Exception as e:75 print(e)76 return False77 else:78 return True79 def adb_shell(self, command, args, include_stderr=False):80 result = self.driver.execute_script('mobile: shell', {81 'command': command,82 'args': args,83 'includeStderr': include_stderr,84 'timeout': 500085 })...
automation_practice_form.py
Source:automation_practice_form.py
...9browser.element('[value=Male]').parent_element.click()10browser.element('#userNumber').type('9151001010')11browser.element('#dateOfBirthInput').click()12browser.element('option[value="1999"]').click()13browser.elements('.react-datepicker__month-select option').element_by(text('December')).click()14browser.element('.react-datepicker__day--031:not(.react-datepicker__day--outside-month)').click()15browser.element('#subjectsInput').type('Maths').press_enter()16browser.elements('[for^=hobbies-checkbox]').element_by(text('Sports')).click()17# TODO browser.elements('#uploadPicture')18browser.element('#currentAddress').type('Current Address')19browser.element('#state input').scroll_to().type('NCR').press_enter()20browser.element('#city input').type('Delhi').press_enter().press_enter()21browser.element('.modal-content').should(have.text('Thanks for submitting the form'))22elements = browser.elements('td')23elements.element_by(text('Student Name')).following_sibling.should(have.text('Ivan Ivanov'))24elements.element_by(text('Student Email')).following_sibling.should(have.text('ivanov@mail.ru'))25elements.element_by(text('Gender')).following_sibling.should(have.text('Male'))26elements.element_by(text('Mobile')).following_sibling.should(have.text('9151001010'))27elements.element_by(text('Date of Birth')).following_sibling.should(have.text('31 December,1999'))28elements.element_by(text('Subjects')).following_sibling.should(have.text('Maths'))29elements.element_by(text('Hobbies')).following_sibling.should(have.text('Sports'))30# TODO elements.element_by(text('Picture')).following_sibling.should(have.text('pic.png'))31elements.element_by(text('Address')).following_sibling.should(have.text('Current Address'))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!