Rayrun
← Back to Discord Forum

Help catching exceptions and logic flow

maharishipachecoposted in #help-playwright
Open in Discord
maharishipacheco
maharishipacheco
  1. I am struggling with the right logic flow with my code. I am visiting a page, that so far, can produce several outcomes. I am not sure how to account for all of them while optimizing my code.

So here is the process in words.

  1. Enter a loop I will not leave until I am on the right url. 2.Visit a specific url. In an ideal world, there will be several redirections until I land on a page with title X. But before this, there is another Page Title that always come first, but at times it just get stuck in there...
  2. Several possible outcomes can come from this: 3.1 - The page never loads and I get an error "Title = Problem loading page" 3.2 - The page goes through its redirects but gets stuck on one right before the final redirect. Title = Loyalty 3.3 - The page loads fine and I end in the correct title. Title = Final Prod 3.4 - My proxy is dead and I end up on a page with title "Access Denied" 3.5 - Something goes wrong with firefox loading my proxy and I end up with several different types of Errors ("Err_timed_out", "NS_ERROR_NET_TIMEOT", "NS_ERROR_PROXY_BAD_GATEWAY", and many more like this)

What I am doing right now is I have a try/except block to visit the url and have an assertion to expect the final page with the correct title, to check if any other thing happens prior to this I have a long timeout before . After the timeout expires, if the page is not the corrct one, it then proceeds to my exceptions, but my problem with this approach is that everytime something goes wrong, my code still has to wait for the entire duration of my timeout before realizing something went wrong. So here are my questions:

  1. How could I quickly catch any of the possibilities mentioned above without having to wait?
  2. How can I consolidate all the different proxy, network errors I keep getting so I cancatch them all, vs what I am doing right now which is wait for a new error and add it to my exception block?
loop = False
        while not loop:
            context = await browser.new_context(proxy = {"server": f'http://{port}',"username":user,"password":pwd})
            try:
                await page.goto(f"http://specific url")
                await expect(page).to_have_title("Final Prod"),timeout=timeout)
            except Exception as e:
                if "ERR_TIMED_OUT" in str(e) or "ERR_INVALID_AUTH_CREDENTIALS" in str(e) or "NS_ERROR_PROXY_CONNECTION_REFUSED" in str(e) or "NS_ERROR_NET_TIMEOUT" in str(e)\
                        or "NS_ERROR_CONNECTION_REFUSED" in str(e) or "NS_ERROR_FAILURE" in str(e) or "NS_ERROR_PROXY_CONNECTION_REFUSED" in str(e) or "NS_ERROR_FAILURE" in str(e)\
                        or "NS_ERROR_PROXY_BAD_GATEWAY" in str(e):
                    print(f"{str(e)}, trying new proxy. Bad proxy {proxy}")
                    try:
                        self.remove_bad_proxy(proxy)
                    except:
                        pass
                    proxy = random.choice(proxy_list)
                    port = f'{proxy.split(":")[0]}:{proxy.split(":")[1]}'
                    user = proxy.split(":")[2]
                    pwd = proxy.split(":")[3]
                    await context.close()
            elif:
                page_title = await page.title()
                if page_title == "Loyalty" or page_title == "Problem loading page" or page_title == "Access Denied":
                    try:
                        self.remove_bad_proxy(proxy)
                    except:
                        pass
                    proxy = random.choice(proxy_list)
                    port = f'{proxy.split(":")[0]}:{proxy.split(":")[1]}'
                    user = proxy.split(":")[2]
                    pwd = proxy.split(":")[3]
                    await context.close()
                else:
                    loop = True

This thread is trying to answer question "How can I quickly catch any of the possibilities mentioned without having to wait? How can I consolidate all the different proxy, network errors I keep getting so I can catch them all, versus what I am doing right now which is wait for a new error and add it to my exception block?"

2 replies
maharishipacheco
maharishipacheco

Important caveats. Loading the final page may sometimes take 10-15 sec but all other errors (except for the stuck redirect) will happen almost immediately. So I would want to optimize my code so it recognizes any of the other outcomes right away and if no known error occurs then to wait the full length of my timeout to see if it simply got stuck on Title==Loyalty.

maharishipacheco
maharishipacheco

Also. How can i format my code block to make it readable (with colors and stuff)?

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].