Best Python code snippet using playwright-python
EtteplanLibrary.py
Source:EtteplanLibrary.py  
...12        self.playwright = sync_playwright().start()13        self.browser = self.playwright.chromium.launch(headless=True).new_context()14        self.page = self.browser.new_page()15        self.page.goto("https://www.etteplan.com/")16        self.page.wait_for_load_state()17        if self.page.title() != "Engineering company with a Difference | Etteplan":18            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <'Engineering company with a Difference | Etteplan'>")19        #self.page.screenshot(path="screenshot_homepage.png")20        logging.info(f"Landed on page <{self.page.title()}>")21    def navigate_to_homepage(self):22        '''A keyword to navigate to Etteplan homepage using hardcoded url. '''23        self.page.goto("https://www.etteplan.com/")24        self.page.wait_for_load_state()25        if self.page.title() != "Engineering company with a Difference | Etteplan":26            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <'Engineering company with a Difference | Etteplan'>")27        logging.info(f"Landed on page <{self.page.title()}>")28    def get_country_id(self):29        cid = self.page.get_attribute('//html', 'lang')30        logging.info(f"Country id is <{cid}>")31        return cid32    def navigate_to_section_our_services(self):33        self.page.wait_for_selector('//ul[@class="menu-main level-0"]//a[@href="/services"]')34        self.page.click('//ul[@class="menu-main level-0"]//a[@href="/services"]')35        self.page.wait_for_load_state()36    def our_services_section_should_be_open(self):37        if self.page.title() != "Our Services | Etteplan":38            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <'Our Services | Etteplan'>")39        logging.info(f"Landed on page <{self.page.title()}>")40    def navigate_to_section_solutions_for_you(self):41        self.page.wait_for_selector('//ul[@class="menu-main level-0"]//a[@href="/solutions"]')42        self.page.click('//ul[@class="menu-main level-0"]//a[@href="/solutions"]')43        self.page.wait_for_timeout(2000) #The page was slow today44        self.page.wait_for_load_state45    def solutions_section_should_be_open(self):46        if self.page.title() != "Solutions for you | Etteplan":47            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Solutions for you | Etteplan>")48        logging.info(f"Landed on page <{self.page.title()}>")49    50    51    def navigate_to_section_references(self):52        #self.page.wait_for_selector('//ul[@class="menu-main level-0"]//a[@href="/references"]')53        self.page.click('//ul[@class="menu-main level-0"]//a[@href="/references"]')54        self.page.wait_for_load_state()55    def references_section_should_be_open(self):56        #self.page.wait_for_timeout(5000)57        if self.page.title() != "References | Etteplan":58            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <References | Etteplan>")59        logging.info(f"Landed on page <{self.page.title()}>")60    61    62    def navigate_to_section_careers(self):63        self.page.wait_for_selector('//ul[@class="menu-main level-0"]//a[@href="/careers"]')64        self.page.click('//ul[@class="menu-main level-0"]//a[@href="/careers"]')65        self.page.wait_for_load_state()66    67    def careers_section_should_be_open(self):68        if self.page.title() != "Careers with a difference | Etteplan":69            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Careers with a difference | Etteplan>")70        logging.info(f"Landed on page <{self.page.title()}>")71    def navigate_to_section_inverstors(self):72        self.page.wait_for_selector('//ul[@class="menu-main level-0"]//a[@href="/investors"]')73        self.page.click('//ul[@class="menu-main level-0"]//a[@href="/investors"]')74        self.page.wait_for_load_state()75    def investors_section_should_be_open(self):76        if self.page.title() != "Investors | Etteplan":77            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Investors | Etteplan>")78        logging.info(f"Landed on page <{self.page.title()}>")79    def navigate_to_section_about_us(self):80        self.page.wait_for_selector('//ul[@class="menu-main level-0"]//a[@href="/about-us"]')81        self.page.click('//ul[@class="menu-main level-0"]//a[@href="/about-us"]')82        self.page.wait_for_load_state()83    def about_us_section_should_be_open(self):84        if self.page.title() != "About Etteplan | Etteplan":85            raise AssertionError(f"The title was <{self.page.title}> when expected title was <'About Etteplan | Etteplan'>")86        logging.info(f"Landed on page <{self.page.title()}>")87    def navigate_to_section_contact(self):88        self.page.wait_for_selector('//ul[@class="menu-main level-0"]//a[@href="/contact-us"]')89        self.page.click('//ul[@class="menu-main level-0"]//a[@href="/contact-us"]')90        self.page.wait_for_load_state()91    def contact_us_section_should_be_open(self):92        if self.page.title() != "Contact us | Etteplan":93            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <'Contact us | Etteplan'>")94        logging.info(f"Landed on page <{self.page.title()}>")95    def navigate_to_testing_and_test_laboratory(self):96        97        self.page.click('//a[@href="https://www.etteplan.com/services/testing-and-test-laboratory"]')98        self.page.wait_for_load_state()99    def testing_and_test_laboratory_page_should_be_open(self):100        if self.page.title() != "Testing and Test Laboratory | Etteplan":101            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Testing and Test Laboratory | Etteplan>")102        logging.info(f"Landed on page <{self.page.title()}>")103    def navigate_to_software_test_automation(self):104        '''A keyword to navigate to "Software test automation" page from "Testing and test laboratory"'''105        self.page.wait_for_selector('//span[@class="field-content"] //a[@href="/services/testing-and-test-laboratory/software-test-automation"]')106        self.page.click('//span[@class="field-content"] //a[@href="/services/testing-and-test-laboratory/software-test-automation"]')107        self.page.wait_for_load_state()108    def software_test_automation_page_should_be_open(self):109        if self.page.title() != "Software Test Automation Service| Etteplan | Etteplan":110            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Software Test Automation Service| Etteplan | Etteplan>")111        logging.info(f"Landed on page <{self.page.title()}>")112    def choose_suomi_from_dropdown(self):113        '''A keyword to navigate to Etteplan's Finnish homepage using the dropdown menu in site header. 114        In case the current page is the same one as the seeked one, the keyword is skipped.'''115        url = self.page.get_attribute('//head/link[1]', 'href')116        logging.info(f"Test started from <{url}>")117        if url != 'https://www.etteplan.com/fi':118            self.page.click('//div[@class="language-switcher"]')119            self.page.wait_for_selector('//li[@class="langcode-fi"]', timeout=5000)120            self.page.click('//li[@class="langcode-fi"]')121            self.page.wait_for_load_state122            self.page.wait_for_timeout(2000)  # Not reliable without the wait123        else:124            logging.info("The test was skipped because current url was the same as the requested one")125    126    def finnish_homepage_should_be_open(self):127        if self.page.title() != "Erilainen suunnittelutoimisto | Etteplan":128            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Erilainen suunnittelutoimisto | Etteplan>")129        logging.info(f"Landed on page <{self.page.title()}>")130    def choose_sverige_from_dropdown(self):131        '''A keyword to navigate to Etteplan's Swedish homepage using the dropdown menu in site header. 132        In case the current page is the same one as the seeked one, the keyword is skipped.'''133        url = self.page.get_attribute('//head/link[1]', 'href')134        logging.info(f"Test started from <{url}>")135        if url != 'https://www.etteplan.com/sv':136            self.page.click('//div[@class="language-switcher"]')137            self.page.wait_for_selector('//li[@class="langcode-sv"]', timeout=5000)138            self.page.click('//li[@class="langcode-sv"]')139            self.page.wait_for_load_state140            self.page.wait_for_timeout(2000) # Not reliable without the wait141        else:142            logging.info("The test was skipped because current url was the same as the requested one")143    def swedish_homepage_should_be_open(self):144        if self.page.title() != "Ingenjörsföretag som gör skillnad | Etteplan":145            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Ingenjörsföretag som gör skillnad | Etteplan>")146        logging.info(f"Landed on page <{self.page.title()}>")147    def choose_nederland_from_dropdown(self):148        '''A keyword to navigate to Etteplan's Dutch homepage using the dropdown menu in site header. 149        In case the current page is the same one as the seeked one, the keyword is skipped.'''150        url = self.page.get_attribute('//head/link[1]', 'href')151        logging.info(f"Test started from <{url}>")152        if url != 'https://www.etteplan.com/nl':153            self.page.wait_for_selector('//div[@class="language-switcher"]')154            self.page.click('//div[@class="language-switcher"]')155            self.page.wait_for_selector('//li[@class="langcode-nl"]', timeout=5000)156            self.page.click('//li[@class="langcode-nl"]')157            self.page.wait_for_load_state158            self.page.wait_for_timeout(2000) # Not reliable without the wait159        else:160            logging.info("The test was skipped because current url was the same as the requested one")161    162    def dutch_homepage_should_be_open(self):163        text = self.page.inner_text('//span[contains(text(),"Nederland")]')164        if text != "Nederland":165            raise AssertionError(f"The chosen country was <{text}> when expected country was <Nederland>")166        logging.info(f"Landed on page <{self.page.title()}>")167    def choose_deutchland_from_dropdown(self):168        '''A keyword to navigate to Etteplan's German homepage using the dropdown menu in site header. 169        In case the current page is the same one as the seeked one, the keyword is skipped.'''170        url = self.page.get_attribute('//head/link[1]', 'href')171        logging.info(f"Test started from <{url}>")172        if url != 'https://www.etteplan.com/de':173            self.page.click('//div[@class="language-switcher"]')174            self.page.wait_for_selector('//li[@class="langcode-de"]', timeout=5000)175            self.page.click('//li[@class="langcode-de"]')176            self.page.wait_for_load_state177            self.page.wait_for_timeout(2000) # Not reliable without the wait178        else:179            logging.info("The test was skipped because current url was the same as the requested one")180    def german_homepage_should_be_open(self):181        if self.page.title() != "Engineering Unternehmen mit dem gewissen Unterschied | Etteplan":182            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Engineering Unternehmen mit dem gewissen Unterschied | Etteplan>")183        logging.info(f"Landed on page <{self.page.title()}>")184    def choose_danmark_from_dropdown(self):185        '''A keyword to navigate to Etteplan's Danish homepage using the dropdown menu in site header. 186        In case the current page is the same one as the seeked one, the keyword is skipped.'''187        url = self.page.get_attribute('//head/link[1]', 'href')188        logging.info(f"Test started from <{url}>")189        if url != 'https://www.etteplan.com/dk':190            self.page.click('//div[@class="language-switcher"]')191            self.page.wait_for_selector('//li[@class="langcode-dk"]', timeout=5000)192            self.page.click('//li[@class="langcode-dk"]')193            self.page.wait_for_load_state194            self.page.wait_for_timeout(2000) # Not reliable without the wait195        else:196            logging.info("The test was skipped because current url was the same as the requested one")197    def danish_homepage_should_be_open(self):198        text = self.page.inner_text('//span[contains(text(),"Danmark")]')199        if text != "Danmark":200            raise AssertionError(f"The chosen country was <{text}> when expected country was <Danmark>")201        logging.info(f"Landed on page <{self.page.title()}>")202    def choose_polska_from_dropdown(self):203        '''A keyword to navigate to Etteplan's Poish homepage using the dropdown menu in site header. 204        In case the current page is the same one as the seeked one, the keyword is skipped.'''205        url = self.page.get_attribute('//head/link[1]', 'href')206        logging.info(f"Test started from <{url}>")207        if url != 'https://www.etteplan.com/pl':208            self.page.click('//div[@class="language-switcher"]')209            self.page.click('//li[@class="langcode-pl"]')210            self.page.wait_for_selector('//li[@class="langcode-pl"]', timeout=5000)211            self.page.wait_for_load_state212            self.page.wait_for_timeout(2000) # Not reliable without the wait213        else:214            logging.info("The test was skipped because current url was the same as the requested one")215    def polish_homepage_should_be_open(self):216        if self.page.title() != "Engineering company with a Difference - biuro inżynierskie | Etteplan":217            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Engineering company with a Difference - biuro inżynierskie | Etteplan>")218        logging.info(f"Landed on page <{self.page.title()}>")219    def choose_global_english_from_dropdown(self):220        '''A keyword to navigate to Etteplan's English homepage using the dropdown menu in site header. 221        In case the current page is the same one as the seeked one, the keyword is skipped.'''222        223        url = self.page.get_attribute('//head/link[1]', 'href')224        logging.info(f"Test started from <{url}>")225        if url != 'https://www.etteplan.com/':226            self.page.wait_for_selector('//div[@class="language-switcher"]', timeout=5000)227            self.page.click('//div[@class="language-switcher"]')228            self.page.wait_for_selector('//li[@class="langcode-en"]', timeout=5000)229            self.page.click('//li[@class="langcode-en"]')230            self.page.wait_for_load_state(timeout=5000)231            self.page.wait_for_timeout(2000) # Not reliable without the wait232        else:233            logging.info("The test was skipped because current url was the same as the requested one")234    def english_homepage_should_be_open(self):235        if self.page.title() != "Engineering company with a Difference | Etteplan":236            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Engineering company with a Difference | Etteplan>")237        logging.info(f"Landed on page <{self.page.title()}>")238    def choose_chinese_from_dropdown(self):239        '''A keyword to navigate to Etteplan's Chinese homepage using the dropdown menu in site header. 240        In case the current page is the same one as the seeked one, the keyword is skipped.'''241        url = self.page.get_attribute('//head/link[1]', 'href')242        logging.info(f"Test started from <{url}>")243        if url != 'https://www.etteplan.com/cn':244            self.page.click('//div[@class="language-switcher"]')...scraper.py
Source:scraper.py  
...74            # print(type(element_list))75        else:76            # arrow down77            self.page.click(self.banner)78            self.page.wait_for_load_state("networkidle");79            for i in list(range(0,count_none)):80                self.page.keyboard.press("PageDown")81                self.page.wait_for_load_state("networkidle");82                time.sleep(random.uniform(0, 2))83                self.page.keyboard.press("PageDown")84                time.sleep(random.uniform(0, 2))85                self.page.wait_for_load_state("networkidle");86                time.sleep(random.uniform(0, 2))87                self.page.keyboard.press("PageDown")88                time.sleep(random.uniform(0, 2))89                self.page.wait_for_load_state("networkidle");90                time.sleep(random.uniform(0, 2))91                self.page.keyboard.press("PageDown")92                time.sleep(random.uniform(0, 2))93                self.page.wait_for_load_state("networkidle");94                time.sleep(random.uniform(0, 2))95            # fetch itmes again96            elements = self.page.query_selector_all('//*/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[2]/div/div/div[6]/div/div/div')97            # compare list then append98            len_elments = len(elements)99            root_elments = count_100            # take to elment list101            if root_elments < len_elments:102                element_list.extend(elements[root_elments-1:len_elments])103            # call fetchnew104            self.fetchnew(filename,element_list)105        return element_list106    def fetech_db(self):107        # get last item108        file_name = ''109        cursor = db.vehicles.find_one()110        print(cursor.get('brand'))111        file = cursor.get('file_name')112        db.vehicles.delete_many({})113        return file114    115    # def feed_db(self):116        117    def get_filenames(self,elements_list):118        count_none = 0119        count_ = 0120        file_image_names = []121        for element in elements_list:122            # print(element.inner_html())123            if element.inner_html()== '<div></div>':124                count_none+=1125            else:126                img = element.inner_html()127                # print(img)128                soup = BeautifulSoup(img ,"html.parser")129                src = soup.find('img').attrs['src']130                # print(soup.find('img'))131                file_name_jpg = src.split('_n.')[0].split('/')[-1]+'_n'132                # file_name_png = src.split('.png')[0].split('/')[-1]133                if re.match(r"^(data:image\/png)",file_name_jpg):134                    # print('outlier-loader')135                    pass136                elif re.match(r"(\d+_)(\d+_)(\d+_n)", file_name_jpg):137                    # print('yes')138                    file_image_names.append(file_name_jpg)139                    count_+=1140        return file_image_names, count_,count_none141    def scrape_newest(self):142        self.page.wait_for_load_state()143        self.page.goto(self.url,wait_until="networkidle")144        self.page.click(self.filter_selector)145        self.page.wait_for_load_state("networkidle");146        self.page.click(self.distance_selector,force=True)147        self.page.wait_for_load_state()148        self.page.click(self.distance_selector_sub,force=True)149        self.page.wait_for_load_state()150        self.page.click(self.apply_btn,force=True)151        self.page.wait_for_load_state("networkidle");152        self.page.click(self.banner)153        self.page.wait_for_load_state("networkidle");154        """155        fetch mongo db :156            if last update one is from yesterday:157                 reset database158                 fill scraped data in last 2 hours159            there is no data:160                fill scraped data in last 2 hours161            else162                get last updated item filename from mongodb163                scroll till find that filename164                update the firestore/mongodb165                    image-filename166                    last-updated167                    content168        """169        self.page.keyboard.press("PageDown")170        self.page.wait_for_load_state("networkidle");171        time.sleep(random.uniform(0, 3))172        self.page.keyboard.press("PageDown")173        self.page.wait_for_load_state("networkidle");174        time.sleep(random.uniform(0, 3))175        elements_xlist = self.page.query_selector_all('//*/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[2]/div/div/div[6]/div/div/div')176        # file_ = '239554633_4643887242311324_4332611350569084027_n'177        file_ = self.fetech_db()178        elements_list= self.fetchnew(filename=file_,element_list=elements_xlist)179        outputs = elements_list[0:self.index]180        outputs_html = [ element.inner_html() for element in outputs]181        outputs_text = [element.inner_text() for element in outputs]182        self.fetch_data(outputs_html,outputs_text)183    def write_data(self,data):184        for x,data_point in enumerate(data):185            # print(data_point)186            result=db.vehicles.insert_one(data_point)187            #Step 4: Print to the console the ObjectID of the new document188    def close_connection(self):189        self.page.close()190        # self.browser.close()191    192    def fetch_data(self,outputs_html=[],outputs_text=[]):193        self.page.close()194        last_time = ''195        data = []196        for i,element in enumerate(outputs_html):197            # self.browser.clear_cookies()198            # self.browser.clear_permissions()199            # page.set_default_timeout(240000)200            # page.set_default_timeout(240000)201            # print(f'{i}')202            page = self.browser.new_page()203            page.set_default_timeout(240000)204            if '<div></div>' not in element:205        206                soup = BeautifulSoup(element ,"html.parser")207                src = soup.find('img').attrs['src']208                if re.match(r"^(data:image\/png)",src):209                    print('outlier-loader')210                    pass211                else:212                    link = 'https://www.facebook.com'+soup.find('a').attrs['href']213                    link = "/".join(link.split('/')[0:6])214                    # print(link)215                    file_name = src.split('_n.')[0].split('/')[-1]+'_n'216                    # print('--------------------------')217                    # print(f"src :{src}")218                    # print(f"file_name : {file_name}")219                    page.goto(link,wait_until="networkidle")220                    page.wait_for_load_state()221                    if 'unavailable_product' not in page.url:222                        price , brand , location, distance = None,None,None,None223                        try :224                            page.wait_for_selector("//span[contains(text(),'Listed')]")225                            listed_time = page.inner_text("//span[contains(text(),'Listed')]")226                            location_listed = page.inner_text("//span[contains(text(),'Listed')]/a/span")227                            subtract = listed_time.replace(location_listed,'')228                            time_listed = re.sub(r'^(Listed)|(\sin)', '', subtract)229                            last_time = time_listed230                            if len(outputs_text[i].split('\n'))==4:231                                price , brand , location, distance = outputs_text[i].split('\n')232                                data.append({233                                        'brand' : brand,234                                        'img_src': src,...CountriesLibrary.py
Source:CountriesLibrary.py  
...11        self.playwright = sync_playwright().start()12        self.browser = self.playwright.firefox.launch(headless=False).new_context()13        self.page = self.browser.new_page()14        self.page.goto("https://www.etteplan.com/")15        self.page.wait_for_load_state()16        if self.page.title() != "Engineering company with a Difference | Etteplan":17            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <'Engineering company with a Difference | Etteplan'>")18        #self.page.screenshot(path="screenshot_homepage.png")19        logging.info(f"Landed on page <{self.page.title()}>")20    def navigate_to_homepage(self):21        '''A keyword simply to navigate to the home page. May be needed as a starting point fo further testing.'''22        self.page.goto("https://www.etteplan.com/")23        self.page.wait_for_load_state()24        if self.page.title() != "Engineering company with a Difference | Etteplan":25            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <'Engineering company with a Difference | Etteplan'>")26        logging.info(f"Landed on page <{self.page.title()}>")27    def choose_suomi_from_dropdown(self):28        '''A keyword to navigate to Etteplan's Finnish homepage using the dropdown menu in site header. 29        In case the current page is the same one as the seeked one, the keyword is skipped.'''30        url = self.page.get_attribute('//head/link[1]', 'href')31        logging.info(f"Test started from <{url}>")32        if url != 'https://www.etteplan.com/fi':33            self.page.click('//div[@class="language-switcher"]')34            self.page.click('//li[@class="langcode-fi"]')35            self.page.wait_for_load_state36            self.page.wait_for_timeout(3000)  #Fix the wait37        else:38            logging.info("The test was skipped because current url was the same as the requested one")39    40    def finnish_homepage_should_be_open(self):41        if self.page.title() != "Erilainen suunnittelutoimisto | Etteplan":42            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Erilainen suunnittelutoimisto | Etteplan>")43        logging.info(f"Landed on page <{self.page.title()}>")44    def choose_sverige_from_dropdown(self):45        '''A keyword to navigate to Etteplan's Swedish homepage using the dropdown menu in site header. 46        In case the current page is the same one as the seeked one, the keyword is skipped.'''47        url = self.page.get_attribute('//head/link[1]', 'href')48        logging.info(f"Test started from <{url}>")49        if url != 'https://www.etteplan.com/sv':50            self.page.click('//div[@class="language-switcher"]')51            self.page.click('//li[@class="langcode-sv"]')52            self.page.wait_for_load_state53            self.page.wait_for_timeout(3000) #Fix the wait54        else:55            logging.info("The test was skipped because current url was the same as the requested one")56    def swedish_homepage_should_be_open(self):57        if self.page.title() != "Ingenjörsföretag som gör skillnad | Etteplan":58            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Ingenjörsföretag som gör skillnad | Etteplan>")59        logging.info(f"Landed on page <{self.page.title()}>")60    def choose_nederland_from_dropdown(self):61        '''A keyword to navigate to Etteplan's Dutch homepage using the dropdown menu in site header. 62        In case the current page is the same one as the seeked one, the keyword is skipped.'''63        url = self.page.get_attribute('//head/link[1]', 'href')64        logging.info(f"Test started from <{url}>")65        if url != 'https://www.etteplan.com/nl':66            self.page.wait_for_selector('//div[@class="language-switcher"]')67            self.page.click('//div[@class="language-switcher"]')68            self.page.wait_for_selector('//li[@class="langcode-nl"]')69            self.page.click('//li[@class="langcode-nl"]')70            self.page.wait_for_load_state71        else:72            logging.info("The test was skipped because current url was the same as the requested one")73    74    def dutch_homepage_should_be_open(self):75        text = self.page.inner_text('//span[contains(text(),"Nederland")]')76        if text != "Nederland":77            raise AssertionError(f"The chosen country was <{text}> when expected country was <Nederland>")78        logging.info(f"Landed on page <{self.page.title()}>")79    def choose_deutchland_from_dropdown(self):80        '''A keyword to navigate to Etteplan's German homepage using the dropdown menu in site header. 81        In case the current page is the same one as the seeked one, the keyword is skipped.'''82        url = self.page.get_attribute('//head/link[1]', 'href')83        logging.info(f"Test started from <{url}>")84        if url != 'https://www.etteplan.com/de':85            self.page.click('//div[@class="language-switcher"]')86            self.page.click('//li[@class="langcode-de"]')87            self.page.wait_for_load_state88            self.page.wait_for_timeout(3000) #Fix the wait89        else:90            logging.info("The test was skipped because current url was the same as the requested one")91    def german_homepage_should_be_open(self):92        if self.page.title() != "Engineering Unternehmen mit dem gewissen Unterschied | Etteplan":93            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Engineering Unternehmen mit dem gewissen Unterschied | Etteplan>")94        logging.info(f"Landed on page <{self.page.title()}>")95    def choose_danmark_from_dropdown(self):96        '''A keyword to navigate to Etteplan's Danish homepage using the dropdown menu in site header. 97        In case the current page is the same one as the seeked one, the keyword is skipped.'''98        url = self.page.get_attribute('//head/link[1]', 'href')99        logging.info(f"Test started from <{url}>")100        if url != 'https://www.etteplan.com/dk':101            self.page.click('//div[@class="language-switcher"]')102            self.page.click('//li[@class="langcode-dk"]')103            self.page.wait_for_load_state104        else:105            logging.info("The test was skipped because current url was the same as the requested one")106    def danish_homepage_should_be_open(self):107        text = self.page.inner_text('//span[contains(text(),"Danmark")]')108        if text != "Danmark":109            raise AssertionError(f"The chosen country was <{text}> when expected country was <Danmark>")110        logging.info(f"Landed on page <{self.page.title()}>")111    def choose_polska_from_dropdown(self):112        '''A keyword to navigate to Etteplan's Poish homepage using the dropdown menu in site header. 113        In case the current page is the same one as the seeked one, the keyword is skipped.'''114        url = self.page.get_attribute('//head/link[1]', 'href')115        logging.info(f"Test started from <{url}>")116        if url != 'https://www.etteplan.com/pl':117            self.page.click('//div[@class="language-switcher"]')118            self.page.click('//li[@class="langcode-pl"]')119            self.page.wait_for_load_state120            self.page.wait_for_timeout(3000) #Fix the wait121        else:122            logging.info("The test was skipped because current url was the same as the requested one")123    def polish_homepage_should_be_open(self):124        if self.page.title() != "Engineering company with a Difference - biuro inżynierskie | Etteplan":125            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Engineering company with a Difference - biuro inżynierskie | Etteplan>")126        logging.info(f"Landed on page <{self.page.title()}>")127    def choose_global_english_from_dropdown(self):128        '''A keyword to navigate to Etteplan's English homepage using the dropdown menu in site header. 129        In case the current page is the same one as the seeked one, the keyword is skipped.'''130        131        url = self.page.get_attribute('//head/link[1]', 'href')132        logging.info(f"Test started from <{url}>")133        if url != 'https://www.etteplan.com/':134            self.page.wait_for_selector('//div[@class="language-switcher"]', timeout=5000)135            self.page.click('//div[@class="language-switcher"]')136            #self.page.wait_for_selector('//li[@class="langcode-en"]']', timeout=5000)137            self.page.click('//li[@class="langcode-en"]')138            self.page.wait_for_load_state(timeout=5000)139            self.page.wait_for_timeout(3000) #Fix the wait140        else:141            logging.info("The test was skipped because current url was the same as the requested one")142    def english_homepage_should_be_open(self):143        if self.page.title() != "Engineering company with a Difference | Etteplan":144            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Engineering company with a Difference | Etteplan>")145        logging.info(f"Landed on page <{self.page.title()}>")146    def choose_chinese_from_dropdown(self):147        '''A keyword to navigate to Etteplan's Chinese homepage using the dropdown menu in site header. 148        In case the current page is the same one as the seeked one, the keyword is skipped.'''149        url = self.page.get_attribute('//head/link[1]', 'href')150        logging.info(f"Test started from <{url}>")151        if url != 'https://www.etteplan.com/cn':152            self.page.click('//div[@class="language-switcher"]')153            self.page.click('//li[@class="langcode-cn"]')154            self.page.wait_for_load_state155            self.page.wait_for_timeout(3000) #Fix the wait156        else:157            logging.info("The test was skipped because current url was the same as the requested one")158    def chinese_homepage_should_be_open(self):159        if self.page.title() != "ä¸ä¼ä¸åçå·¥ç¨è®¾è®¡å
¬å¸ | Etteplan":160            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <ä¸ä¼ä¸åçå·¥ç¨è®¾è®¡å
¬å¸ | Etteplan>")161        logging.info(f"Landed on page <{self.page.title()}>")162    def navigate_to_current_country_homepage_by_clicking_logo(self):163        cid = self.page.get_attribute('//html', 'lang')   # KESKEN164        logging.debug(f"country id is <{cid}>")165        if cid != 'en':166            exp_url = f"https://www.etteplan.com/{cid}"167            logging.debug(f"expected url <{exp_url}>")168        else:169            exp_url = f"https://www.etteplan.com/"170        self.page.wait_for_selector('//div[@id="block-etteplan-branding"]//a[@class="site-logo"]')171        self.page.click('//div[@id="block-etteplan-branding"]//a[@class="site-logo"]')172        url = self.page.get_attribute('//head/link[1]', 'href')173        if exp_url != url:174            raise AssertionError(f"The url was expected to be <{exp_url}> but was <{url}>")175    def get_country_id(self):176        177        cid = self.page.get_attribute('//html', 'lang')178        logging.info(f"Country id is <{cid}>")179    def navigate_to_section_contact(self):180        self.page.wait_for_selector('//ul[@class="menu-main level-0"]//a[@href="/contact-us"]')181        self.page.click('//ul[@class="menu-main level-0"]//a[@href="/contact-us"]')182        self.page.wait_for_load_state()183        if self.page.title() != "Contact us | Etteplan":184            raise AssertionError(f"The title was <{self.page.title()}> when expected title was <Contact us | Etteplan>")185        logging.info(f"Landed on page <{self.page.title()}>")186    # Teardown187    def close_browser(self):188        """Closes browser and stops Playwright."""189        self.browser.close()190        self.playwright.stop()...main.py
Source:main.py  
...29    def login(self, page: Page):30        with page.expect_popup() as popup_info:31            page.click(Paths.popup_open_path)32        popup = popup_info.value33        popup.wait_for_load_state("networkidle")34        username = popup.locator(Paths.username_path)35        username.type(Options.login or "")36        popup.wait_for_timeout(100)37        password = popup.locator(Paths.password_path)38        password.type(Options.password or "")39        login_button = popup.locator(Paths.login_button_path)40        login_button.focus()41        popup.wait_for_load_state("networkidle")42        login_button.click()43        try:44            popup.wait_for_load_state("networkidle")45            # popup.wait_for_timeout(2000)46            popup.close()47        except:48            pass49    def change_language(self, page: Page):50        page.wait_for_load_state("networkidle")51        # page.locator(Paths.change_language_selector).click()52        page.locator(Paths.change_language_path).click()53        page.wait_for_load_state("networkidle")54        pass55    def go_to_schedule_visit(self, page: Page):56        page.wait_for_timeout(1000)57        page.goto(Paths.schedule_visit_link)58    def print_days(self, days):59        for day in days:60            title = day.query_selector(".visitListDate").inner_text()61            dates = day.query_selector_all(".slot-time")62            print(f"==================== {title} ====================")63            for date in dates:64                print(f"- {date.inner_text()}")65    def go_to_visits(self, page: Page):66        # self.go_to_schedule_visit(page)67        page.wait_for_load_state("networkidle")68        # page.goto(Paths.schedule_visit_link)69        # page.wait_for_load_state("networkidle")70        # page.query_selector(Paths.specialist_button_selector).click()71        # page.wait_for_load_state("networkidle")72        page.goto(Paths.specialist_url)73        page.wait_for_load_state("networkidle")74        page.query_selector(Paths.search_selector).click()75        page.wait_for_load_state("networkidle")76        page.wait_for_timeout(5000)77        days = page.query_selector_all("app-visit-list>div:has(.visitListDate)")78        self.print_days(days)79    def main(self):80        with sync_playwright() as p:81            browser_func = getattr(p, Options.browser)82            browser = browser_func.launch(headless=Options.headless)83            context = browser.new_context()84            page = context.new_page()85            page.goto(Paths.main_page)86            self.login(page)87            self.change_language(page)88            page.wait_for_load_state("networkidle")89            self.go_to_visits(page)90            title = page.title()91            browser.close()92if __name__ == "__main__":...LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
