O
O
Olga Belova2018-04-19 18:37:34
Python
Olga Belova, 2018-04-19 18:37:34

How to set up an argument for a different URL value when testing on different environments?

There is a specific smoke test script that runs every day.
You need to set different arguments to test this script on different environments
. Also, on some environments, the password will change

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import Select


driver = webdriver.Chrome("C:\File_Path")
base_url = localUrl

driver.get(base_url)
driver.maximize_window()

username = driver.find_element_by_id("Username")
password = driver.find_element_by_id("Password")

username.send_keys("username")
password.send_keys("pass")

driver.find_element_by_name("Login").click()

below is the environment on which the test is run every day
localUrl = local.com
developmentUrl = development.com
productionUrl = production.com
How to implement it so as not to change base_url = localUrl manually

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav Grigoriev, 2018-04-19
@Spring_Way

If you need a settings file, then you can add the following function:

def load_config(path):
    config_path = os.path.join(path, 'config.json')
    if not os.path.exists(config_path):
        print('Not found config.json in {}'.format(path))
        return
    with open(config_path, 'r') as fp:
        config = json.load(fp)
    return config

Now you can get the settings object like this:
import os

script_path = os.path.dirname(os.path.abspath(__file__))
config = load_config(script_path)

If the settings file is found and contains something like this:
{
  "localUrl": "local.com",
  "developmentUrl": "development.com"
  "productionUrl": "production.com"
}

Then you can get parameters in the script in this way:
driver.get(config['localUrl'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question