Best Python code snippet using lettuce_webdriver_python
test_vectors.py
Source:test_vectors.py  
...16    b.get(url)17    b.select_param('variable', 'Tidal Gauge')18    time.sleep(4) # wait for the map to stabilise19    b.wait(jquery('svg circle'))20    markers = b.find_elements_by_jquery('svg circle')21    print markers22    # check we have the right number of markers23    assert len(markers) == 1324    for m in markers:25        assert m.is_displayed()26        assert m.get_attribute('stroke') == 'white'27        assert m.get_attribute('fill') == 'black'28def test_click_gauge(b, url):29    b.get(url)30    b.select_param('variable', 'Tidal Gauge')31    time.sleep(4) # wait for the map to stabilise32    b.wait(jquery('svg circle'))33    marker = b.find_element_by_jquery('svg circle:first')34    assert marker.get_attribute('stroke') == 'white'35    marker.click()36    assert marker.get_attribute('stroke') == 'red'37    # this will be the first value in the config file38    input = b.find_element_by_id('tidalgauge')39    assert input.get_attribute('value') == "Fiji - Suva"40    input = b.find_element_by_id('tgId')41    assert input.get_attribute('value') == 'IDO70063'42    util.clear_cache('SEA')43    b.submit()44    b.wait(output('SEA'))45@pytest.mark.bug20046def test_hover_gauge(b, url):47    b.get(url)48    b.select_param('variable', 'Tidal Gauge')49    time.sleep(4) # wait for the map to stabilise50    b.wait(jquery('svg circle'))51    marker = b.find_element_by_jquery('svg circle:first')52    chain = ActionChains(b)53    chain.move_to_element(marker)54    chain.perform()55    elems = b.find_elements_by_jquery('svg text')56    assert len(elems) == 157    assert elems[0].text == "Fiji - Suva"58def test_gauge_offscreen(b, url):59    b.get(url)60    b.select_param('variable', 'Tidal Gauge')61    select_region(b, 'niue')62    with pytest.raises(InvalidSelectorException):63        # with all the circles offscreen there's nothing to select64        b.find_element_by_jquery('svg circle')65    select_region(b, 'samoa')66    assert len(b.find_elements_by_jquery('svg circle')) > 067def pan_map(b, offsetx, offsety):68    map = b.find_element_by_id('map')69    act = ActionChains(b)70    act.move_to_element(map)71    act.click_and_hold()72    act.move_by_offset(offsetx, offsety)73    act.release()74    act.perform()75    time.sleep(1)76@pytest.mark.bug18377@pytest.mark.bug19578@pytest.mark.unstable79def test_gauge_bug195(b, url):80    b.get(url)81    b.select_param('variable', 'Tidal Gauge')82    b.wait(jquery('svg circle'))83    select_region(b, 'fiji')84    assert len(b.find_elements_by_jquery('svg circle')) == 285    LEFT_PAN = 5086    RIGHT_PAN = 37587    pan_map(b, -LEFT_PAN, 0)88    assert len(b.find_elements_by_jquery('svg circle')) == 289    pan_map(b, LEFT_PAN + RIGHT_PAN, 0)90    assert len(b.find_elements_by_jquery('svg circle')) == 291@pytest.mark.bug18392@pytest.mark.unstable93@pytest.mark.parametrize(('lat', 'lon'), [94    (-18, 177),95    (-16, -179),96])97def test_display_markers_big_movements(b, url, lat, lon):98    b.get(url)99    b.select_param('variable', 'Salinity')100    b.select_param('plottype', 'Sub-surface Cross-section')101    b.find_element_by_id('latitude').send_keys(str(lat) + Keys.TAB)102    b.find_element_by_id('longitude').send_keys(str(lon) + Keys.TAB)103    time.sleep(1)104    # svg > polygon is important, else you match both the point and the105    # definition106    assert len(b.find_elements_by_jquery('svg > polygon')) == 1107    select_region(b, 'fiji')108    assert len(b.find_elements_by_jquery('svg > polygon')) == 1109    # asymmetric because it makes the test a little more stable110    LEFT_PAN = -250111    RIGHT_PAN = 300112    pan_map(b, LEFT_PAN, 0)113    assert len(b.find_elements_by_jquery('svg > polygon')) == 0114    pan_map(b, RIGHT_PAN, 0)115    assert len(b.find_elements_by_jquery('svg > polygon')) == 1116    pan_map(b, RIGHT_PAN, 0)117    assert len(b.find_elements_by_jquery('svg > polygon')) == 0118    pan_map(b, LEFT_PAN, 0)119    assert len(b.find_elements_by_jquery('svg > polygon')) == 1120@pytest.mark.bug183121@pytest.mark.parametrize(('lat', 'lon'), [122    (-18, 177),123    (-16, -178),124])125def test_display_markers_small_movements(b, url, lat, lon):126    b.get(url)127    select_region(b, 'fiji')128    b.select_param('variable', 'Salinity')129    b.select_param('plottype', 'Sub-surface Cross-section')130    b.find_element_by_id('latitude').send_keys(str(lat) + Keys.TAB)131    b.find_element_by_id('longitude').send_keys(str(lon) + Keys.TAB)132    time.sleep(1)133    # svg > polygon is important, else you match both the point and the134    # definition135    assert len(b.find_elements_by_jquery('svg > polygon')) == 1136    LEFT_PAN = 20137    pan_map(b, -LEFT_PAN, 0)138    assert len(b.find_elements_by_jquery('svg > polygon')) == 1139    pan_map(b, LEFT_PAN, 0)140    assert len(b.find_elements_by_jquery('svg > polygon')) == 1141    pan_map(b, LEFT_PAN, 0)142    assert len(b.find_elements_by_jquery('svg > polygon')) == 1143    pan_map(b, -LEFT_PAN, 0)144    assert len(b.find_elements_by_jquery('svg > polygon')) == 1145@pytest.mark.parametrize(('lon'), [146    (177),147    (184),148    (-178),149    (-215),150])151def test_lon_clamping(b, url, lon):152    b.get(url)153    b.select_param('variable', 'Salinity')154    b.select_param('plottype', 'Sub-surface Cross-section')155    # this needs to be valid to attempt parsing156    b.find_element_by_id('latitude').send_keys('0')157    longitude = b.find_element_by_id('longitude')158    longitude.send_keys(str(lon) + Keys.TAB)...css_selector_steps.py
Source:css_selector_steps.py  
...8import logging9log = logging.getLogger(__name__)10@wait_for11def wait_for_elem(browser, sel):12    return find_elements_by_jquery(browser, sel)13def load_script(browser, url):14    """Ensure that JavaScript at a given URL is available to the browser."""15    if browser.current_url.startswith('file:'):16        url = 'https:' + url17    browser.execute_script("""18    var script_tag = document.createElement("script");19    script_tag.setAttribute("type", "text/javascript");20    script_tag.setAttribute("src", arguments[0]);21    document.getElementsByTagName("head")[0].appendChild(script_tag);22    """, url)23    sleep(1)24def find_elements_by_jquery(browser, selector):25    """Find HTML elements using jQuery-style selectors.26    Ensures that jQuery is available to the browser; if it gets a27    WebDriverException that looks like jQuery is not available, it attempts to28    include it and reexecute the script."""29    try:30        return browser.execute_script("""return ($ || jQuery)(arguments[0]).get();""", selector)31    except WebDriverException as e:32        if e.msg.startswith(u'$ is not defined'):33            load_script(browser, "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js")34            return browser.execute_script("""return ($ || jQuery)(arguments[0]).get();""", selector)35        else:36            raise37def find_element_by_jquery(step, browser, selector):38    """Find a single HTML element using jQuery-style selectors."""39    elements = find_elements_by_jquery(browser, selector)40    assert_true(step, len(elements) > 0)41    return elements[0]42def find_parents_by_jquery(browser, selector):43    """Find HTML elements' parents using jQuery-style selectors.44    In addition to reliably including jQuery, this also finds the pa"""45    try:46        return browser.execute_script("""return ($ || jQuery)(arguments[0]).parent().get();""", selector)47    except WebDriverException as e:48        if e.msg.startswith(u'$ is not defined'):49            load_script(browser, "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js")50            return browser.execute_script("""return ($ || jQuery)(arguments[0]).parent().get();""", selector)51        else:52            raise53@step(r'There should be an element matching \$\("(.*?)"\)$')54def check_element_by_selector(step, selector):55    elems = find_elements_by_jquery(world.browser, selector)56    assert_true(step, elems)57@step(r'There should be an element matching \$\("(.*?)"\) within (\d+) seconds?$')58def wait_for_element_by_selector(step, selector, seconds):59    elems = wait_for_elem(world.browser, selector, timeout=int(seconds))60    assert_true(step, elems)61@step(r'There should be exactly (\d+) elements matching \$\("(.*?)"\)$')62def count_elements_exactly_by_selector(step, number, selector):63    elems = find_elements_by_jquery(world.browser, selector)64    assert_true(step, len(elems) == int(number))65@step(r'I fill in \$\("(.*?)"\) with "(.*?)"$')66def fill_in_by_selector(step, selector, value):67    elem = find_element_by_jquery(step, world.browser, selector)68    elem.clear()69    elem.send_keys(value)70@step(r'I submit \$\("(.*?)"\)')71def submit_by_selector(step, selector):72    elem = find_element_by_jquery(step, world.browser, selector)73    elem.submit()74@step(r'I check \$\("(.*?)"\)$')75def check_by_selector(step, selector):76    elem = find_element_by_jquery(step, world.browser, selector)77    if not elem.is_selected():78        elem.click()79@step(r'I click \$\("(.*?)"\)$')80def click_by_selector(step, selector):81    # No need for separate button press step with selector style.82    elem = find_element_by_jquery(step, world.browser, selector)83    elem.click()84@step(r'I follow the link \$\("(.*?)"\)$')85def click_by_selector(step, selector):86    elem = find_element_by_jquery(step, world.browser, selector)87    href = elem.get_attribute('href')88    world.browser.get(href)89@step(r'\$\("(.*?)"\) should be selected$')90def click_by_selector(step, selector):91    # No need for separate button press step with selector style.92    elem = find_element_by_jquery(step, world.browser, selector)93    assert_true(step, elem.is_selected())94@step(r'I select \$\("(.*?)"\)$')95def select_by_selector(step, selector):96    option = find_element_by_jquery(step, world.browser, selector)97    selectors = find_parents_by_jquery(world.browser, selector)98    assert_true(step, len(selectors) > 0)99    selector = selectors[0]100    selector.click()101    sleep(0.3)102    option.click()103    assert_true(step, option.is_selected())104@step(r'There should not be an element matching \$\("(.*?)"\)$')105def check_element_by_selector(step, selector):106    elems = find_elements_by_jquery(world.browser, selector)107    assert_false(step, elems)108__all__ = [109    'wait_for_element_by_selector',110    'fill_in_by_selector',111    'check_by_selector',112    'click_by_selector',113    'check_element_by_selector',...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!!
