DEV Community


Posted on

Growing everyday episode-1(2019-11-26)

Dear folks,
Today suddenly I think I should write down what did I learnt everyday when going to work in automation for specific or coding in general.

This post tends to be in short. Hope it will not spam you or anything.

So today I had to write UI test for new tab page of CocCoc. You can refer to this : coccoc-newtab

1.Find element by css_selector:
This should be an old thing. But today I learn how to find an element by css_selector which does not contains certain class name.
For example like below:

ZEN_NEWS_ITEM_CSS_SELECTOR = 'div[class] > a:not(.qc-link)[href]:not(.context-content)'
Enter fullscreen mode Exit fullscreen mode

for find element like:
Alt Text

2.String literals in python over 3.6:

For someone using Python, you can use string literals when dealing with strings like this

utm_type = 'utm_source'

string_literal_example = f'this is string literal: {utm_type}'
Enter fullscreen mode Exit fullscreen mode


For example, assert certain text in the given text:

    for utm_type in ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term']:
        assert'\b(\w*{utm_type}=\w*)\b(?!\s*$).+', current_url) is not None
Enter fullscreen mode Exit fullscreen mode

(text contains utm_type which is variable, and follow by some character)

4.Click on element using javascript:
Sometimes, when click using selenium, you got the selenium intercepted exception.
In that case, you could use javascript click in selenium.

    def click_on_any_zen_element(self, driver):
        driver.execute_script('arguments[0].click();', self.new_tab_zen_elem.find_any_zen_element(driver))
Enter fullscreen mode Exit fullscreen mode

The self.new_tab_zen_elem.find_any_zen_element(driver) is element type:

    def find_any_zen_element(self, driver):
        return self.wait_for_element(driver).until(ec.presence_of_element_located(NewTabZenLocators.ZEN_NEWS_ITEM))
Enter fullscreen mode Exit fullscreen mode

5.Scroll down page using javascript in selenium:

Sometimes, move to element function in native selenium does not work.
So you might need to use javascript:

    def scroll_to_with_scroll_height(self, driver):
        driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
Enter fullscreen mode Exit fullscreen mode

That's it.
Hope it helps.

Top comments (0)