How to use test_xpath method in SeleniumBase

Best Python code snippet using SeleniumBase

test_selenium.py

Source:test_selenium.py Github

copy

Full Screen

1"""Module containing tests for the selenium utility functions"""2import os3from datetime import datetime, timedelta4from typing import Tuple, Any5from unittest import TestCase, main6from unittest.mock import Mock, patch7from selenium import webdriver8from selenium.common.exceptions import TimeoutException9from selenium.webdriver.common.by import By10from judah.utils.selenium import (11 get_web_driver,12 get_html_element_by_xpath,13 visit_website,14 wait_for_download_to_complete,15 wait_for_element_to_load,16 WebDriverOptions17)18_PARENT_FOLDER = os.path.dirname(__file__)19_MOCK_ASSET_FOLDER_PATH = os.path.join(os.path.dirname(_PARENT_FOLDER), 'assets')20class TestSeleniumUtilities(TestCase):21 """Tests for the selenium utility functions"""22 def test_get_web_driver(self):23 """24 Should return a working instance of a chrome webdriver25 FIXME: Unfortunately, I am unable to check if any of the options have been applied appropriately26 """27 download_location = os.path.join(_MOCK_ASSET_FOLDER_PATH, 'csv')28 options = WebDriverOptions(downloads_folder_location=download_location)29 driver = get_web_driver(options)30 self.assertIsInstance(driver, webdriver.Chrome)31 driver.close()32 driver.quit()33 def test_visit_website(self):34 """35 Should make a GET request to the URL passed to it36 """37 mock_driver = Mock(spec=webdriver.Chrome)38 mock_driver.get = Mock()39 url = 'http://example.com'40 visit_website(mock_driver, website_url=url)41 mock_driver.get.assert_called_once_with(url=url)42 def test_get_html_element_by_xpath(self):43 """44 Should get the html element by given xpath, waiting for it only up to a given timeout45 and giving up46 """47 start_time = datetime.now()48 mock_driver = Mock(spec=webdriver.Chrome)49 timeout_in_seconds = 250 seconds_before_element_shows_up = 151 test_xpath = 'hello_world'52 test_element = {'foo': 'bar'}53 def mock_find_element_by_xpath(xpath: str):54 """Waits for a given time before returning an element"""55 if datetime.now() > start_time + timedelta(seconds=seconds_before_element_shows_up):56 return test_element57 return False58 mock_driver.find_element_by_xpath = Mock()59 mock_driver.find_element_by_xpath.side_effect = mock_find_element_by_xpath60 element = get_html_element_by_xpath(driver=mock_driver, xpath=test_xpath, timeout=timeout_in_seconds)61 mock_driver.find_element_by_xpath.assert_called_with(xpath=test_xpath)62 self.assertEqual(element, test_element)63 def test_timed_out_get_html_element_by_xpath(self):64 """65 Should raise TimeoutException if the element takes longer than the timeout setting to show up66 """67 mock_driver = Mock(spec=webdriver.Chrome)68 timeout_in_seconds = 269 test_xpath = 'hello_world'70 mock_driver.find_element_by_xpath.return_value = False71 self.assertRaises(TimeoutException, get_html_element_by_xpath, driver=mock_driver, xpath=test_xpath,72 timeout=timeout_in_seconds)73 @patch('os.path.isfile')74 @patch('os.path.exists')75 def test_wait_for_download_to_complete(self, mock_os_path_exists, mock_os_path_is_file):76 """77 Should wait for download to complete before returning78 """79 expected_file_path = 'dummy.csv'80 timeout = 281 seconds_before_download_completes = 182 mock_os_path_is_file.return_value = True83 start_time = datetime.now()84 def mock_os_path_exists_side_effect(file_name: str):85 """Waits for a given time before returning True"""86 if datetime.now() > start_time + timedelta(seconds=seconds_before_download_completes):87 return True88 return False89 mock_os_path_exists.side_effect = mock_os_path_exists_side_effect90 self.assertLessEqual((datetime.now() - start_time).total_seconds(), seconds_before_download_completes)91 wait_for_download_to_complete(expected_file_path=expected_file_path, timeout=timeout)92 self.assertGreater((datetime.now() - start_time).total_seconds(), seconds_before_download_completes)93 @patch('os.path.isfile')94 @patch('os.path.exists')95 def test_wait_for_download_to_complete_not_file(self, mock_os_path_exists, mock_os_path_is_file):96 """97 Should raise IOError if the file is not an actual file98 """99 expected_file_path = 'dummy.csv'100 timeout = 2101 mock_os_path_exists.return_value = True102 mock_os_path_is_file.return_value = False103 self.assertRaises(IOError, wait_for_download_to_complete,104 expected_file_path=expected_file_path, timeout=timeout)105 @patch('judah.utils.selenium.EC')106 def test_wait_for_element_to_load(self, mock_ec):107 """108 Should wait for the element to load before returning109 """110 start_time = datetime.now()111 mock_driver = Mock(spec=webdriver.Chrome)112 timeout_in_seconds = 2113 seconds_before_element_shows_up = 1114 test_xpath = 'hello_world'115 test_element = {'foo': 'bar'}116 def mock_presence_of_element_located(search_item: Tuple[Any]):117 """Waits for a given time before returning an element"""118 if datetime.now() > start_time + timedelta(seconds=seconds_before_element_shows_up):119 return test_element120 return False121 mock_ec.presence_of_element_located = Mock()122 mock_ec.presence_of_element_located.return_value = mock_presence_of_element_located123 self.assertLessEqual((datetime.now() - start_time).total_seconds(), seconds_before_element_shows_up)124 wait_for_element_to_load(driver=mock_driver, xpath=test_xpath, timeout=timeout_in_seconds)125 self.assertGreater((datetime.now() - start_time).total_seconds(), seconds_before_element_shows_up)126 mock_ec.presence_of_element_located.assert_called_once_with((By.XPATH, test_xpath))127if __name__ == '__main__':...

Full Screen

Full Screen

ExtractionPath_tb.py

Source:ExtractionPath_tb.py Github

copy

Full Screen

1'''2Created on Feb 8, 201134@author: 33As5'''6import unittest7import ExtractionPath8import Level9import Vertex10from lxml import etree11from StringIO import StringIO1213class EPTest(unittest.TestCase):14 def case(self, h, l, r, html, xpath):15 ep = ExtractionPath.ExtractionPath(html=html, xpath=xpath)16 return {'h':h, 'l':l, 'r':r, 'html':html, 'xpath':xpath, 'ep':ep}17 18 def setUp(self):19 '''Rough sketch of how to make a more thorough test suite'''20 self.test_html = "<html><body><div>this is a test</div><div>not part of the test</div></body></html>"21 self.test_xpath = "/html/body/div[1]"22 self.extract_ep1 = ExtractionPath.ExtractionPath(html=self.test_html, xpath=self.test_xpath)23 self.testcase_dict = {0: self.case(3, 0, 2,24 "<html><body><div>this is a test</div><div>not part of the test</div></body></html>",25 "/html/body/div[1]")}26 self.testcase=0;27 self.vertex_cond = Vertex.Vertex('l')28 self.vertex_cond.addLabel('div')29 self.vertex_cond.addLabel('f')30 31 def test_height(self):32 self.assertEqual(self.testcase_dict[self.testcase]['ep'].height(), self.testcase_dict[self.testcase]['h'])33 #self.assertEqual(self.extract_ep1.height(), 3) #ep13435 def test_left(self):36 self.assertEqual(self.testcase_dict[self.testcase]['ep'].left(), self.testcase_dict[self.testcase]['l'])37 #self.assertEqual(self.extract_ep1.left(), 0) #ep138 39 def test_right(self):40 self.assertEqual(self.testcase_dict[self.testcase]['ep'].right(), self.testcase_dict[self.testcase]['r'])41 #self.assertEqual(self.extract_ep1.right(), 2) #ep142 43 def test_level(self):44 #TODO45 pass46 47 def test_epath_to_xpath(self):48 xpath = self.testcase_dict[self.testcase]['ep'].epath_to_xpath()49 test_tree = etree.parse(StringIO(self.testcase_dict[self.testcase]['html']))50 elem_expected = test_tree.xpath(self.testcase_dict[self.testcase]['xpath'])51 elem_found = test_tree.xpath(xpath)52 self.assertEqual(elem_found, elem_expected)53 #xpath = self.extract_ep1.epath_to_xpath()54 #test_tree = etree.parse(StringIO(self.test_html))55 #elem_expected = test_tree.xpath(self.test_xpath)56 #elem_found = test_tree.xpath(xpath)57 #self.assertEqual(elem_found, elem_expected)58 59 def test_cond(self):60 xc = self.extract_ep1.cond(self.vertex_cond)61 xc_expected = "[local-name()='"+self.vertex_cond.getTag()+"']"+"[not (preceding-sibling::*)]"+"[not (following-sibling::*)]"62 self.assertEqual(xc, xc_expected)63 64 def test_xpath_to_epath(self):65 #TODO66 pass67 6869if __name__ == "__main__":70 #import sys;sys.argv = ['', 'Test.testheight']71 #unittest.main()72 suite = unittest.TestLoader().loadTestsFromTestCase(EPTest) ...

Full Screen

Full Screen

test_all.py

Source:test_all.py Github

copy

Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3#4# Copyright (c), 2016-2019, SISSA (International School for Advanced Studies).5# All rights reserved.6# This file is distributed under the terms of the MIT License.7# See the file 'LICENSE' in the root directory of the present8# distribution, or http://opensource.org/licenses/MIT.9#10# @author Davide Brunato <brunato@sissa.it>11#12if __name__ == '__main__':13 import unittest14 import os15 from xmlschema.tests import print_test_header16 from xmlschema.tests import test_cases, test_etree, test_helpers, \17 test_meta, test_models, test_regex, test_resources, test_xpath18 from xmlschema.tests.validation import test_validation, test_decoding, test_encoding19 def load_tests(loader, tests, pattern):20 tests.addTests(loader.loadTestsFromModule(test_cases))21 validators_dir = os.path.join(os.path.dirname(__file__), 'validators')22 tests.addTests(loader.discover(start_dir=validators_dir, pattern=pattern or 'test_*.py'))23 tests.addTests(loader.loadTestsFromModule(test_validation))24 tests.addTests(loader.loadTestsFromModule(test_decoding))25 tests.addTests(loader.loadTestsFromModule(test_encoding))26 tests.addTests(loader.loadTestsFromModule(test_etree))27 tests.addTests(loader.loadTestsFromModule(test_helpers))28 tests.addTests(loader.loadTestsFromModule(test_meta))29 tests.addTests(loader.loadTestsFromModule(test_models))30 tests.addTests(loader.loadTestsFromModule(test_regex))31 tests.addTests(loader.loadTestsFromModule(test_resources))32 tests.addTests(loader.loadTestsFromModule(test_xpath))33 return tests34 print_test_header()...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run SeleniumBase automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful