core

Seting up playwright and other various attributes of its object Like Page and Locators

source

retry_async

 retry_async (max_retries:int=3, backoff_base:float=1.5)

Decorator for retrying an asynchronous function upon exception. Retries the execution of an async function a specified number of times (max_retries) with an exponential backoff delay between attempts (backoff_base). If all attempts fail, the exception from the last attempt is raised.

setup_browser

Setup browser by opening up chrome and all the other objects


source

setup_browser

 setup_browser (*args, n:int=1, stealth:bool=False, **kwargs)

Browser context manager returns n pages and stealth for mode


source

BrowserCleanupError

Common base class for all non-exit exceptions.


source

PageCreationError

Common base class for all non-exit exceptions.


source

BrowserResources

 BrowserResources
                   (pw_obj:Optional[playwright.async_api._generated.Playwr
                   ight]=None, brow:Optional[playwright.async_api._generat
                   ed.Browser]=None, ctx:Optional[playwright.async_api._ge
                   nerated.BrowserContext]=None,
                   pages:List[playwright.async_api._generated.Page]=None,
                   is_valid:bool=False)
try:
        async with setup_browser(n=0, stealth=False) as obj:
                if obj.is_valid:
                        ...
except ValueError as e:
        assert str(e) == "Invalid number of pages: 0"
async with setup_browser(n=1, stealth=False) as obj:
        if obj.is_valid:
            pg = await obj.pages[0].goto('http://example.org')
            assert  pg.status == 200

Page’s Monkey patching


source

wait_page

 wait_page (page:playwright.async_api._generated.Page, pause=50,
            timeout=5000)

moneky patching Page.wait_page. Wait until page and frames are ready to be loaded

async with setup_browser(n=1, headless=True) as obj:
        if obj.is_valid:
                pg = await obj.pages[0].goto('http://example.org')
                await obj.pages[0].wait()
                assert  pg.status == 200

source

find_ele

 find_ele (page:playwright.async_api._generated.Page, locator:str)

To locate elements on a web page using a given locator and return a list of those elements. Logs error if the locator object is not present and returns None.

async with setup_browser(n=1, headless=True) as obj:
    if obj.is_valid:
        await obj.pages[0].goto('https://nbdev.fast.ai/')
        await obj.pages[0].wait()
        assert len(await obj.pages[0].find_ele("//span[contains(text(), 'Blog')]") ) != 0
        assert await obj.pages[0].find_ele("//span[contains(text(), 'blah')]") is None
Error find : @https://nbdev.fast.ai/  for //span[contains(text(), 'blah')] :->  Element not found.  

source

Locator’s Monkey patching


source

left_click

 left_click (element:playwright.async_api._generated.Locator,
             timeout=5000)

for a given element it performs single click operation

url = 'https://solveit.fast.ai/'
async with setup_browser(n=1, headless=False) as obj:
    if obj.is_valid:
        await obj.pages[0].goto(url)
        await obj.pages[0].wait()
        loc = await obj.pages[0].find_ele("//a[contains(text(), 'Course Details')]") 
        assert len(loc) != 0
                        
        await loc[0].left_click()
        assert url != obj.pages[0].url

source

enter_txt

 enter_txt (element:playwright.async_api._generated.Locator, text:str,
            timeout:int=5000)

For a given element of type Locator, it types the specified text

async with setup_browser(n=1, headless=True) as obj:
    if obj.is_valid:
        await obj.pages[0].goto('https://nbdev.fast.ai/')
        await obj.pages[0].wait()
        loc = await obj.pages[0].find_ele("//button[1]") 
        assert len(loc) != 0
                        
        await loc[0].left_click()
        await obj.pages[0].wait()
        inp =  await obj.pages[0].find_ele("//input") 
        await inp[0].left_click()
        await inp[0].enter_txt("type_text")

        assert  await inp[0].input_value() == "type_text"

source

get_text

 get_text (element:playwright.async_api._generated.Locator,
           timeout:int=5000)

Gets the text content of an Locator element

async with setup_browser(n=1, headless=True) as obj:
    if obj.is_valid:
        await obj.pages[0].goto('https://nbdev.fast.ai/')
        await obj.pages[0].wait()
        loc = await obj.pages[0].find_ele('//a[@role="button"][1]') 
        assert len(loc) != 0
                        
        assert await loc[0].get_text() == "Get started"