Webdriver in Selenoid - selenium

I am creating testing of my website in pytest using selenium and selenoid.
I have a class that inherits from BaseCase and for example when I want to open a webpage I write super().open(URL). Or, if I want to click on an element I write: self.click(element_selector).
I am a bit confused, however, on why most other examples I find online first have to create a webdriver, and only then can they do actions such as open and click, thru it. Whereas I can just access it thru class object (self.click()).
I understand that this has to do with my using selenoid. However, I am not quite sure how it all fits together. I have had a lot of trouble finding an explanation online, as every time I try to type in the words selenoid and webdriver together, google assumes I mean selenium. I can't find any related results. Anyone have an explanation on this? (Or, even a better search term to use than Selenoid Webdriver Pytest or What do I use instead of Webdriver in Selenoid?

I suggest implementing your tests in the way like tutorials suggest, at least until you'll feel experienced. In this way, you will do your tasks in the way most other people do.
So I suggest do not use some libraries classes inheritance, but creating webdriver instance and using it. (But still, it's your choice and up to you). I cannot add anything more without seeing your code..
Selenium + Selenoid
Selenoid behaves like Selenium Grid.
If your Selenoid started on localhost:4444, just
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=chrome_options
)
driver.get("http://www.google.com")
driver.quit()
Setup Selenoid
(assumed you already have Docker):
1 Download cm from
https://github.com/aerokube/cm/releases
2 Run chmod +x cm
3 Run
./cm selenoid start
And your Selenoid will be ready to accept requests.
Check http://localhost:4444/status.
References
https://aerokube.com/selenoid/latest/
https://www.selenium.dev/documentation/webdriver/remote_webdriver/

Related

Should I quit the browser after each Selenium based automated test?

I'm trying to make my selenium test as atomic and independent of each other as possible so I decided to quit the browser and create a new Webdriver instance after each test runs. This approach made the more sense to me and was reinforced by several threads discussing this issue.
e. g. This answer to a related question:
You are closing the webdriver after one particular test. This is a good approach but you will need to start a new webdriver for each new test that you want to run.
However, I've also come across the opinion that quitting the browser after each test is unnecessary and ineffective.
e. g. Part of this blog about Selenium:
It’s not good practice to load a browser before each test. Rather, it is much better to load a browser before all tests and then close it after all tests are executed, as this will save resources and test execution time.
As I'm pretty new to all of this, I'm struggling to choose between these two. So far the execution time of my tests is not a real concern (as I only have a handful of them) but as I begin to expand my test suite I'm worried that it might become an issue.
Answering straight, factually there is no definite rules to quit or reuse the same browser client while executing the tests using Selenium. Perhaps the decision would be based on the pre-requisites of the testcases.
If your tests are independent, it would be wise to to quit() the current Webdriver and Browser Client instance and create a new instance of the Webdriver and Browser Client after each test runs which will initiate a new and clean WebDriver / Browser Client combination as discussed in closing browser after test pass.
Albeit it would induce some overhead to spawn the new WebDriver / Browser Client combination but that may provide the much needed cushion from CPU and Memory usage as discussed in:
Limit chrome headless CPU and memory usage
Selenium using too much RAM with Firefox
Incase, the tests are not independent and the tests are based on the same session, cookies, etc parameters, reusing the same WebDriver / Browser Client makes sense.
DebanjanB has a great answer.
I am of the cloth that there is no one-sized-fits-all answer.
There is some fun balance to be had. Depending on what framework you are using, you could get fancy. I like pytest for it's unique use of fixtures.
To this end, you could do tests, or sets of tests either way depending on what you need. You could balance browser load times vs execution for what makes sense.
As an example in pytest:
conftest.py:
import pytest
from selenium import webdriver
#pytest.fixture(scope='module')
def module_browser(request):
"""Fixture lasts for an entire file of tests."""
driver = webdriver.Chrome()
def fin():
driver.quit()
request.addfinalizer(fin())
return driver
#pytest.fixture(scope='function')
def function_browser(request):
"""Fixture lasts for just a test function."""
driver = webdriver.Chrome()
def fin():
driver.quit()
request.addfinalizer(fin())
return driver
Now module_browser() lets you get a browser for a whole test module.
funtion_browser() gives you a new browser per test function.
Lets get fancy.. you have a bunch of tests that need to be logged in, and they are doing cosmetic checks on a standard account:
conftest.py continued...
#pytest.fixture(scope='module')
def logged_in_browser(request):
"""Provide a logged in browser for simple tests."""
driver = webdriver.Opera()
# Now go and log this browser in,
# so we can use the logged in state for tests.
log_in_browser(username='RedMage', password='masmune')
def fin():
driver.quit()
request.addfinalizer(fin())
return driver
This is about the same, but lets you have a browser stay open for a few tests, and it's logged in. If logging in takes say 5 seconds, and you have 30 tests that atomically check cosmetic things, you can shave a few minutes.
This flexibility will let you run some tests faster, and some tests in a more clean state. We might need some of each to run a suite and still be able to get efficiency gains on the time. There is no one-sized-fits-all answer.
Utilizing fixture in pytest lets you choose what you want for each test.. if it should be a clean browser, or if it needs to be faster.
Then in the tests we see stuff like this:
test_things.py
def test_logged_out_assets(function_browser):
driver = function_browser # just for clarity here.
driver.get('http://example.com/')
check_some_stuff(driver)
language_subdomain_list = ['www', 'es', 'de', 'ru', 'cz']
#pytest.parametrize(language_subdomain, language_subdomain_list)
def test_logged_out_assets_multlingual(module_browser, language_subdomain):
"""
Check the assets come up on each language subdomain.
This test will run for each of the subdomains as separate tests.
5 in all.
"""
driver = module_browser # for clarity in example.
url = "http://{}.example.com".format(language_subdomain)
driver.get(url)
check_some_stuff(driver)
def test_logged_in_assets(logged_in_browser):
"""
Check specific assets while logged in.
Remember, our web browser already is logged in when we get it!
"""
driver = logged_in_browser # for clarity in example.
check_some_assets(driver)
Py.test Fixtures: https://docs.pytest.org/en/latest/fixture.html

Migrating to Webdriver from Selenium RC

I am migrating from RC to webdriver.
In my existing project I use methods from the Selenium Class like
selenium.click()
selenium.type()
etc.
Do I need to change these to the equivalent webdriver commands, or is there a way i can still use these commands?
I use firefox 12, Eclipse IDE
There is the WebDriverBackedSelenium. Essentially this is a bridge between the RC API and WebDriver API. This will do what you are after, there will be some modification to code, but majority will still be the same. It gives you the flexibility of the WebDriver itself, while keeping old code the same.
It is highly recommended to fully convert your solution to use the WebDriver API directly.
The WebDriver API is constantly being updated, worked on and supported.
The RC API and the "RC-WebDriver-Bridge" (WebDriverBackedSelenium) won't be.
Page on WebDriverBackedSelenium exists here:
http://seleniumhq.org/docs/03_webdriver.html#alternative-back-ends-mixing-webdriver-and-rc-technologies
Sample usage to create a new instance of Firefox:
var driver = new FirefoxDriver();
var selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
selenium.stop();
After creating a WebDriverBackedSelenium instance with a given Driver, one does not have to call start() - as the creation of the Driver already started the session. At the end of the test, stop() should be called instead of the Driver's quit() method.
This is more similar to WebDriver's behaviour - as creating a Driver instance starts a session, yet it has to be terminated explicitly with a call to quit().

what is the difference between selenium.click and driver.click

I have recorded a scenario in Selenium IDE and exported it as a Junit4 Webdriver backed code.
There is a command which uses selenium object and the same thing could be done by driver object.
So I am not able to understand which one to use and when
E.g :
selenium.click("id=gen_info") can also be implemented by
driver.findElement(By.id("gen_info")).click();
Yes I do have a option to have a driver object of specific web browser but then the same thing could be done by using selenium object also.
I suppose that by selenium click u mean something like this:
WebDriver driver = new FirefoxDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
WebDriverBackedSelenium allows those who have test suites using the Selenium-RC to migrate to WebDriver. However it doesn't implement all methods.
In this particular case it should work the same, though WebDriverBacked may be slower

Selenium Firefox Web Driver occasionally not finding an element

I am using the Selenium Firefox Web Driver to run automated NUnit tests and, seemingly at random, it is not able to locate a particular button.
The function it is using is this
driver.FindElement(By.Id("createUser")).Click();
I can't figure out a pattern to why it does and doesn't work at different times. Does anyone have any ideas as to what is going on or what a possible work around might be? I am not using dynamic ids, and I have
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));
in place to cause it to wait up to 15 seconds for the page to fully render.

Selenium 2: getScreenshotAs() mixed up when running several tests in parallel

in a continuous integration build environment when running several Selenium tests in parallel (using Firefox driver) for different applications and each tests records its screenshots after every "action" (e.g. navigating to a page, submitting a form etc.) it seems like that whichever application window pops up that one gets on the top of the z-axis and will have the focus.
So using the method getScreenshotAs() from the Selenium API to record images results in mixed up screenshots sometimes showing one application and sometimes the other application.
Recording the HTML responses with getPageSource() on the other hand seems to work correctly using the Firefox driver instance "bound" to the test.
Is there any solution how to deal with the mixed up image screenshots? Is there a possibility how to ensure that getScreenshotAs() only consideres its own Firefox driver instance? Thanks for any hints!
Peter
I don't know what flavor of selenium you are using but here is a reference to the API that looks like it would fix your problem, but I have never tested it.
http://selenium.googlecode.com/svn/trunk/docs/api/dotnet/index.html
What that link shows is the IWrapDriver which according to the documentation Gets the IWebDriver used to find this element.
So from my understanding you could set your IWebDriver in your method and then wrapit with the IWrapDriver and then use that to reference for you getScreenShotAs();