Django Environment Variables is not recognized - python

Going through a tutorial at Obey The Testing Goat and I'm getting errors using environmental variables. Using Django 1.11, Selenium 3, and Python 3.6.
(py36) C:\testgoat\superlists>STAGING_SERVER=superlists-staging.ottg.eu python manage.py test functional_tests
'STAGING_SERVER' is not recognized as an internal or external command,
operable program or batch file.
If I understand the tutorial correctly, an environmental variable, STAGING_SERVER, is used run tests using a real server instead of Django's test server.
django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException
import time
import unittest
import sys
import os
MAX_WAIT = 10
class NewVisitorTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
staging_server = os.environ.get('STAGING_SERVER')
if staging_server:
setattr(self, 'live_server_url', 'http://' + staging_server)
[...]

Related

How to get Selenium/GeckoDriverManager driver path in Python

Following this issue I encountered yesterday : selenium.common.exceptions.SessionNotCreatedException in Python Selenium using GeckoDriverManager, I would like to know if there was a way to delete at the end of a program execution the driver directory created by GeckoDriverManager.
Here's what I use to put in my codes :
import os, shutil
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
service = FirefoxService(executable_path=GeckoDriverManager().install())
driver = webdriver.Firefox(service=service)
Every time this is executed, GeckoDriverManager creates a folder in my /tmp directory called rust_mozprofile<random chars and digits>. Is there any way or function to get the path to this folder, in order to delete it and its contents, for example using the code below?
def __cleanup(driverDirectoryPath):
if os.path.exists(driverDirectoryPath):
shutil.rmtree(driverDirectoryPath)

Python 3 Selenium Run Multiple Browsers Simultaneously

I'm currently trying to run multiple browsers at the same time with Selenium.
All processes start but don't execute passed functions correctly.
My Code:
from multiprocessing import Pool
# function that creates driver
driver = self.create_driver()
pool.apply_async(launcher.launch_browser, driver)
The launch_browser function is a different Python script that is supposed to run but only a Browser window is opened. (yes, I imported the external script as a module)
You can simply open new browser windows the same way you open the first one, with the same or different browsers:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class Bot:
def __init__(self):
self.firstBrowser = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
self.secondBrowser = webdriver.Firefox()
def gotopage(self):
self.firstBrowser.get("https://www.google.com/")
self.secondBrowser.get("https://stackoverflow.com/")
bot = Bot()
bot.gotopage()

How to launch a Chromium application using Chromedriver?

Is it possible to port this python code to work in Robot Framework?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
chrome_options = Options()
chrome_options.binary_location = 'C:/Program Files (x86)/MyApp.exe'
driver = webdriver.Chrome('C:/Program Files (x86)/chromedriver.exe', chrome_options=chrome_options)
I'm trying to create a chrome webdriver in robot that sends my selenium calls to my chromium application. Is it possible?
I create a python Library see code below but it just launches my app and closes it. I want to be able to make selenium/robot calls to it.
Code for myLibrary.py
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from robot.libraries.BuiltIn import BuiltIn
def pas_webdriver_instance():
se2lib = BuiltIn().get_library_instance('ExtendedSelenium2Library')
chrome_options = Options()
chrome_options.binary_location = 'C:/Program Files (x86)/myapp.exe'
driver = webdriver.Chrome('C:/Program Files (x86)/chromedriver.exe', chrome_options=chrome_options)
You could simply expand Selenium2Library and use your Python code directly. To expand Selenium2Library you can either make a new class that inherits the original library, or get the runnung library's instance on runtime.
Or, if you absolutely want to port this into Robot Framework, you can try out this keyword:
Open Chrome
[Arguments] ${url} ${binary_path}
${chrome_options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${chrome_options} binary_location ${binary_path}
Create Webdriver Chrome chrome_options=${chrome_options}
Go To ${url}
Edit: To answer your question in comments,
That depends on the version of Selenium2Library you're using since the library got reworked heavily in it's last version, but if you're using versions prior to it's version 3, you can see an example of expansion that gets an instance here (see the property _s2l), and an example of class inheritance here (see the declaration of the class).
I haven't looked into expanding the last version of SeleniumLibrary yet so I couldn't tell you for this particular case. Retrieving the library's instance on runtime should still work fine though.

In Python, how to automatically import all nested packages without namespace override? [duplicate]

Why the first code doesn't work while the second does?
First code:
import selenium
driver = selenium.webdriver.Firefox()
AttributeError: 'module' object has no attribute 'webdriver'
Second code:
from selenium import webdriver
driver = webdriver.Firefox()
Nested packages are not automatically loaded; not until you import selenium.webdriver is it available as an attribute. Importing just selenium is not enough.
Do this:
import selenium.webdriver
driver = selenium.webdriver.Firefox()
Sometimes the package itself will import a nested package in the __init__.py package initializer; os imports os.path, so os.path is immediately available even if you import just os.

How to pass a variable from testscript file to common file in automation

For iOS automation, I have a re-usable script file called funclib.py. The file is like :
if “iPad in device :
from iPad_resource import *
else:
from iPhone_resource import *
def fnCreateaccount(email,password,device):
wd.find_element_by_name("Create an Account").click()
wait("Create account”)
wd.find_element_by_xpath(r.email_field()).send_keys(email)
wd.find_element_by_xpath(r.password()).send_keys(password)
wd.find_element_by_name("Create account").click()
wd.find_element_by_name("Agree & Sign up").click()
wait("Create Vault")
if(wd.find_element_by_name(“Created Account").is_displayed()):
tcResPass(" Account is Created")
else:
tcResFail("Error in creating account")
return(email, password)
And I have many Testscript files, For each test script, the device will be different. For example , initial.py is a test script file
import os
from selenium import webdriver
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Device = “iPad 2 Retina”
def CreateNewAccountLogin():
email=generateemail(1,12)
password=get_password(letters,6)
fnCreateNA(email, password, device)
When I run this, I am getting error like :
`device is not defined in funclib.`
How can I pass the device value to funclib.py ? I can’t import the initial.py to funclib, because like that I have around 20 test script files. So I need to import everyone which doesn’t make sense.
Can you please help me.
When you say Device = "iPad 2 Retina", you set a local variable in the initial module called Device. The funclib module and the initial module have different local variable namespaces. To set a variable on funclib, you have to do so explicitly by saying funclib.Device="iPad 2 Retina".

Categories