Migrating to Webdriver from Selenium RC - selenium

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().

Related

setCapability() can be called using FirefoxOptions() and DesiredCapabilities object, what's the difference and where to use whichone

I am trying to set some capabilites on firefox.
I see that using firefox object and DesiredCapabilities object we can do the same thing. What's the difference in both and how to choose which one to use. We can call the same method setCapability() with both objects.
Set the capabilities which were running successfully. But i am confused in options and DesiredCapabilities. What is the difference and relation in both.
FirefoxOptions options = new FirefoxOptions();
options.setCapability();
DesiredCapabilities desiredCap = DesiredCapabilities.firefox();
desiredCap.setCapability();
DesiredCapabilities is the old way of doing things and is currently deprecated. The Options pattern is the new way of doing things and has come into Selenium as part of the work of moving the API across to the new W3C compliant API.
/**
* #deprecated Use {#link #FirefoxDriver(FirefoxOptions)}.
*/
#Deprecated
public FirefoxDriver(Capabilities desiredCapabilities) {
this(new FirefoxOptions(Objects.requireNonNull(desiredCapabilities, "No capabilities seen")));
}
Link to the above code on Github
In Selenium 4 all of the deprecated methods should be getting removed (Of course in practice they may not all be removed straight away) so I would suggest you move over to using FirefoxOptions instead of DesiredCapabilities.

Selenium Webdriver drive two browser

I'm trying to run two distinct actions in two differents browser.
With
Is Selenium WebDriver thread safe? i have learned an important thing: "You /can/ on the other hand instantiate one WebDriver instance for each thread. "
So i have created two thread who instantiate two drivers (first for google, second for yahoo) like this :
public class Thread2 extends Thread{
#Override
public void run() {
File file = new File(Thread2.class.getClassLoader().getResource("chromedriver").getPath());
System.setProperty("webdriver.chrome.driver", file.getPath());
WebDriver driver = new ChromeDriver();
driver.get("http://www.yahoo.com");
while (true) {
driver.findElement(By.id("uh-search-box")).sendKeys("test");
}
}
My Main Class just implementing those two thread and call run function.
There is a way to get two or X browsers running at the same time and excecuting differents task ?
Regards.
More informations at https://github.com/AMimicD/TestSeleniumThread
have you tried to created two different driver variables?
WebDriver driver = new ChromeDriver();
driver.get("http://www.yahoo.com");
WebDriver driver2 = new ChromeDriver();
driver2.get("http://www.whateveryouwant.com");
I assume you want to run your test parallely at the same time. So, in your main method, replace
t1.run();
t2.run();
with
t1.start();
t2.start();
Now you should see both your browsers running at the same time.
Thread.start() is required to actually create a new thread so that the runnable's run method is executed in parallel.
Note: There is a problem with your while condition. The condition is always true and hence it will be an infinite loop. Please fix this in your code.
Integrate your script with TestNG. It will help you to run your script in parallel mode.
Please Refer link to know more about Parallel execution with TestNG.
Thanks!

Session management in Selenium webdriver

I have a class under which I have different methods one of them is login and other are related to the adding product to cart, checkout and shipping.
When I try to run the methods using TestNG in one go, it execute all the methods in different browsers session and I lost my login session.
I want some solution so that either all the methods execute in same browser or I can use the session of login method in other consecutive methods
Thanks in advance
I have same login for both
I am creating new instance of driver in both methods
Here is my code :
public class purchase {
#Test
public void login(){
System.setProperty("webdriver.chrome.driver", "{path}/chromedriver.exe");
WebDriver fd= new ChromeDriver();
fd.get("{domain}/login/");
/*{login script here}*/
}
#Test
public void purchaseItem(){
System.setProperty("webdriver.chrome.driver", "{path}/chromedriver.exe");
WebDriver fd1= new ChromeDriver();
fd1.get("{domain}/travel");
/*add item to cart*/
}
}
It means there might be 2 issue:
1. You have separate login in both the methods
2. You are creating new instance of driver in both methods.
Please share your code and will help you with resolving it.
You have to share the same driver instance between methods. Otherwise, you will have a new session each time, like you described it.
Here you go.. as per your comment - "I am creating new instance of driver in both methods"..
So, if you want to maintain the session, you have to use a single instance of driver in both methods.
You can achieve it using beforeClass method (Use it for driver initializing and login), and write your logic in different test methods.. So it will make sure complete test cycle for the class is 1 and then your session will not be lost.
Let me know if this helps.

Webdriver.quit() ,not working properly when called in #AfterSuite annotation

I have a two "test" in testng XML file, all are running parallel what my issue is that I called a Webdriver.quit() method in the #AfterSuite annotation, but it is only closing the last created browser instance not all opened browsers.
when I call in the #AfterTest annotation it gets closed.
below is my Testng file:
I created the driver in the base class and extend it in test classes.
code for creating driver instance.
#BeforeClass
#Parameters({"deviceName","platformName","platformVersion","udid","browserName","browserVersion","nodeIP","nodePort"})
public void startBrowser(String deviceName, String platformName, String platformVersion, String udid, String browserName, String browserVersion, String nodeIP, String nodePort)
{
DesiredCapabilities capabilities =DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", "chromeDriverPath");
ChromeOptions options = new ChromeOptions();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new ChromeDriver(capabilities);
}
AfterSuite is called once per suite. What you have shown above is one suite executing two tests in parallel. You need to make sure your driver is thread safe.
AfterTest gets called after <test> tag is over - so your webdriver if specific to a thread will get closed.
If you want to close all webdriver in aftersuite, you need to build a list of webdrivers and close each one individually.
Because you have 2 <test>, TestNG is creating 2 instances (with the same after suite method).
But testng consideres a suite method as single in the suite and will only call it once (the first it finds).
That's why it is working with after test method which looks to be the one you should use.
BTW, as you want to close the driver in an after suite method, I suppose you create it in a before suite method which is not supposed to work for the same reasons. Could you detail where/how you create the driver ?

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