Selenium Test Repeat/Toggle - python

I have a test in Selenium IDE with Flow Control which submits a form and repeats the process with label start until I stop the app.
I have exported my code into python WebDriver.
Unfortunately my test only runs once.
How can I duplicate this process on python.
class Buy(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://test.mydomain.com"
def test_buy(self):
driver = self.driver
driver.get(self.base_url + "/Form")
Select(driver.find_element_by_id("DropDownOne")).select_by_visible_text("BLUE")
Select(driver.find_element_by_id("DropDownTwo")).select_by_visible_text("LIGHT")
driver.find_element_by_id("Quantity").send_keys("10")
driver.find_element_by_id("Button").click()

Related

Selenium with Python(POM)

I am new in automation testing. I am learning automation using selenium with python(page object model). From learning YouTube I see that, log in is done for every test case, which is redundant. I would like to login once and execute for multiple test case. This is a sample code.
tests/test_dashboard.py
class TestDashboard:
def test_dashboard(self, setup):
self.driver = setup
self.driver.get(self.base_url)
self.lp = Login(self.driver)
self.lp.set_email(self.user_email)
self.lp.set_password(self.user_password)
self.lp.sign_in()
def test_dashboard_checking_fire(self, setup):
self.driver = setup
self.driver.get(self.base_url)
# self.driver.maximize_window()
self.lp = Login(self.driver)
self.lp.set_email(self.user_email)
self.lp.set_password(self.user_password)
self.lp.sign_in()
You see, for 2 teset cases i have to execute log in everytime. How I can login once and execute that 2 test cases at once? I am using pytest framework.How to do this with pytest. There is a setup method.
tests/conftest.py
#pytest.fixture()
def setup(browser):
if browser == "chrome":
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
elif browser == "firefox":
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
else:
driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()))
return driver
Finally, I solve my Problem. I update my setup function like this:
#pytest.fixture(scope="class", autouse=True)
def setup(request, browser, url):
global driver
# my code
request.cls.driver = driver
yield
driver.close()

Opening Instagram with Selenium webdriver Python

I want to log in to Instagram with selenium webdriber. I wrote a code but It always opens just google chrome page, without going to Instagram page. I have tried to change to .sleep() time but It is always the same.
from selenium import webdriver
import time
class InstagramBot:
def __init__(self, username, password):
self.username = username
self.password = password
self.driver = webdriver.Chrome(executable_path = "C:\Program Files (x86)\xxx\xxx\xxx\chrome.exe")
def closeBrowser(self):
self.driver.close()
def login(self):
driver = self.driver
driver.get("https://www.instagram.com/")
time.sleep(5)
IGBot = InstagramBot("xxx", "yyy")
IGBot.login()
Also I have tried with \chrome.exe and with \chrome, and with "www.instagram.com".
After 5 seconds, I get this error:
selenium.common.exceptions.WebDriverException: Message: Service C:\Program Files (x86)\xxx\xxx\xxxx\chrome.exe unexpectedly exited. Status code was: 0
This line
self.driver = webdriver.Chrome(executable_path = "C:\Program Files (x86)\xxx\xxx\xxx\chrome.exe")
should be
self.driver = webdriver.Chrome(executable_path = "C:\complete\path\to\chromedriver.exe")
Note not to confuse "chrome.exe" with "chromedriver.exe" in Selenium context.

How to go to webpage without delay at "data;" page with Selenium Chromedriver in Python?

I am using Selenium and Chrome webdriver in Python, when I'm running script it opens chrome normally, but it waits in "data;" page around 10 seconds and than go to page normally. Can i skip that waiting time and go to page instantly?
There is my code:
class Instance():
driver=None
url=None
def __init__(self, url):
self.url=url
def runInstance(self):
self.driver = webdriver.Chrome(executable_path = "C:\chromedriver.exe")
self.driver.get(self.url)
instance = Instance("https://www.youtube.com/")
instance.runInstance()

How can I open up a mobile version of Google in Pycharm using Selenium?

I got some code together that lets me view the mobile version of Instagram, but whenever I try to put the code into a class and run the class, it fails. I have one Class for writing my methods, and another Class for calling the methods. When I run the second class, the chromedriver will open chrome, but it won't call the instagram URL.
this is the code that works
from selenium import webdriver
mobile_emulation = { "deviceName": "iPhone X" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation",
mobile_emulation)
driver = webdriver.Chrome(executable_path='/Users/~~/chromedriver', chrome_options=chrome_options)
driver.get("https://instagram.com/")
I tried making a class to hold the code like this
from selenium import webdriver
mobile_emulation = {"deviceName": "iPhone X"}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation",
mobile_emulation)
driver = webdriver.Chrome(executable_path='/Users/~~/chromedriver',
chrome_options=chrome_options)
class IGFrame:
def __init__(self, driver):
self.driver = driver
def open_ig(self):
self.driver.get("https://instagram.com/")
time.sleep(2)
and then this was the class to call the method
from version1.pages.basic_methods import *
class TestSetUp:
def __init__(self, driver):
self.driver = driver
def test_mobileViewOfIG(self):
methods = IGFrame(self.driver)
methods.open_ig()
What I was aiming for was to have the driver setup and the method to call the URL in the IGFrame class, and then I would call the method from the TestSetUp class. What am I doing wrong and what should I do?

Selenium Python Tear Down for Safari. How do you handle this?

So my setup for running Safari .py tests is a little different than those for Chrome and FireFox (examples below). That being said, the tearDown for Safari must be different, but I cannot find any examples or figure it out.
Chrome setup example:
class ChromeAllLinks(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "https://test.foo.com/"
self.verificationErrors = []
self.accept_next_alert = True
Safari setup example:
class SafariAllLinks(unittest.TestCase):
def setUp(self):
import webbrowser
browser = webbrowser.get('safari')
browser.open("https://test.foo.com/")
self.verificationErrors = []
self.accept_next_alert = True
So of course the tearDown for them should be different as well.
Chrome tearDown being used (which works fine of course):
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
And I can show what I am trying for the Safari tearDown, but it isn't working:
def tearDown(self):
browser = webbrowser.quit('safari')
Here is the error being thrown. Says not defined, but it is!
NameError: global name 'webbrowser' is not defined

Categories