Home > Software engineering >  How to run tests one by one using Selenium and Python in one tab?
How to run tests one by one using Selenium and Python in one tab?

Time:11-25

I am pretty new to automated testing, so do not beat me too hard.

So I have 2 separate files with 2 different tests:

  1. Successful login
  2. Creating a request

I can successfully run the Login case, but I can't run the second one, as it requires to be logged in. Is there any way I can run the 1-st one (User logs in) and run the next one in the same browser window without closing this window, so the next test was like a continuation of the previous one?

So, here is a test for Login

from selenium import webdriver

from pages.base_element import BaseElement
from pages.login_page import LoginPage

# Test Setup
browser = webdriver.Chrome()
currentURL = None

# Successful Login
log_page = LoginPage(driver=browser)
log_page.go()   <-------------------------------------------Goes to the Login Page                    
log_page.user_name_field.input_text('[email protected]')

log_page.password_field.input_text('Pass123')
log_page.login_button.click()

And here is for creating a request

from selenium import webdriver
 
from pages.base_element import BaseElement 
from pages.med_page import LoginPage 
from pages.med_page import MedPage
 
# Test 
Setup browser = webdriver.Chrome() 
currentURL = None
 
# Test
med_page = MedPage(driver=browser) 
med_page.go()   <----------------- Must redirect to the Request Page, but redirects to Login as it is a new browser session, so the user is not logged in

CodePudding user response:

Yes you can! For this example, I will name your login code file as "login":

from login import *
from pages.med_page import MedPage


###some request###

When you reference the login script using "from login import *" it runs everything that is on the login script. Hopefully this helps!

  • Related