├── README.md └── tabs.py /README.md: -------------------------------------------------------------------------------- 1 | ## How to handle multiple tabs in automation test in Pytest on [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=Pytest-multiple-tabs) 2 | 3 | If you want to handle multiple tabs using an automation test in Pytest on LambdaTest, you can use the follwing steps. You can refer to sample test repo [here](https://github.com/LambdaTest/pytest-selenium-sample). 4 | 5 | ### Code 6 | 7 | To open a new tab, you can use the following line of code: 8 | 9 | ```python 10 | driver.execute_script("window.open('about:blank','secondtab');") 11 | ``` 12 | 13 | To switch to the new tab, you can use the following line of code: 14 | 15 | ```python 16 | #by using tab name 17 | driver.switch_to.window("secondtab") 18 | ``` 19 | or 20 | ```python 21 | #by using tab id 22 | driver.switch_to.window("2") 23 | ``` 24 | 25 | For full code, refer to `tabs.py`. 26 | ### Run your test 27 | 28 | ```bash 29 | python tabs.py 30 | ``` 31 | 32 | # Links: 33 | 34 | [LambdaTest Community](http://community.lambdatest.com/) 35 | 36 | -------------------------------------------------------------------------------- /tabs.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import sys 3 | 4 | @pytest.mark.usefixtures('driver') 5 | class TestLink: 6 | def scroll_bottom(): 7 | """ 8 | Create new tab, switch to it and switch back to old tab 9 | :return: None 10 | """ 11 | driver.get('https://www.lambdatest.com/') 12 | driver.maximize_window() 13 | driver.execute_script("window.open('about:blank','secondtab');") 14 | driver.switch_to.window("secondtab") 15 | driver.get('https://www.lambdatest.com/') 16 | driver.switch_to.window(1) 17 | assert True 18 | 19 | --------------------------------------------------------------------------------