Switching between 2 windows in selenium webdriver with java - java

I am automating an application using selenium webdriver with java. In that i have to open a browser instance & perform some actions. After that i have to open another browser instance, perform some actions in it & close that instance. Then i have to return the control back to the first browser instance again to perform some other actions.
I tried using :
String winHandleBefore = driver.getWindowHandle();
//then open new instance and perfom the actions
driver.switchTo().window(winHandleBefore);
But this returned an error :
org.openqa.selenium.remote.SessionNotFoundException: no such session
How can i do this? Can anybody help?

When you did driver = new ChromeDriver(); you reinitialized the driver object, which caused the lost of the first window. You can see it by checking the number of window handles after opening the new window
WebDriver driver = new ChromeDriver();
int len = getWindowHandles().size(); // 1 as expected
driver = new ChromeDriver();
len = getWindowHandles().size(); // still 1, has only the new window
To solve this use temporary driver to open the new window
WebDriver tempDriver = new ChromeDriver();
// do some stuff
tempDriver.close();

Related

How to open private and normal window together in selenium webdriver with Java?

My use case is to test Chat application. Verify user A sends message from Normal window and verify that user B received it successfully in incognito mode.
If open same thing in new tab or or window then same login will apply in new tab or window. So I want it to open in incognito mode.
if you need to open a new chrome driver in incognito mode you can do it like this in java:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
WebDriver driver = new ChromeDriver(capabilities);
to open new tab, you could do this :
((JavascriptExecutor) driver).executeScript("window.open()");
remember to switch to it once it's open :
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
and now you have the access to new tab.
Update 1 :
there is this shortcut : CTRL + N
WebElement body = driver.findElement(By.tagName("body"));
body.sendKeys(Keys.CONTROL + "n");

Windows handling in Selenium webdriver using java

I have a scenario as below
1. Login to the application
2. click on a button (say Buy)
3. This will take me to a new window opened with a new URL automatically
4. Perform actions in the new window
5. Quit
Kindly please provide the exact code to work on this. I tried with the available code that exists in the website which didnt work for me
You can try on following pattern:-
Webdriver driver = new ChromeDriver();
driver.get("URL of application");
driver.findElement(By.id("username").sendKeys("user1");
driver.findElement(By.id("password").sendKeys("pass1");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.xPath("xpath of button").click();
//now you can switch to pop-up and accordingly accept or dismiss it
driver.switchTo().alert().accept();
driver.quit();
In case you provide the SO community the URL of application then only complete code can be provided.

Is there a way to display webdriver instance on iframe site

I'm stuck on how can I open the instance of webdriver on my html page using java.
WebDriver driver = new ChromeDriver(); // Open the Application
the code below open the instance of driver chrome.
If you want to start chrome browser, first download chromeDriver from here After that we need to specify this downloaded chromedriver and then start the driver instance
System.setProperty("webdriver.chrome.driver", "\\downloaded driver path");
WebDriver driver = new ChromeDriver();
driver.get("http://URL here/");
Then as said need to switch to frame, so
driver.switchTo().frame("iframe id or name");
//or
driver.switchTo().frame("index like 0, 1");
//or
driver.switchTo().frame(driver.findElement(By.xpath("//iframe"))); //iframe location
//after work in frame is completed, then we need to switch back to default content
driver.switchTo().defaultContent();
Thank You,
Murali

How to use a already opened firefox for testing in Selenium

This declaration
WebDriver driver = new FirefoxDriver();
always opens a new instance window of Firefox. It doesn't use the already opened firefox.
Can anyone let me know how to use a already opened firefox for testing instead of opening a new one?
Use Remote Web Driver
like this .
System.Uri uri = new System.Uri("http://localhost:7055/hub");
WebDriver = new RemoteWebDriver(uri, DesiredCapabilities.Firefox());
it will use the already opened Firefox browser.
you can see the details of this approach in this blog post.
http://www.binaryclips.com/2016/03/selenium-web-driver-in-c-how-to.html
Be careful with that, because in case the driver crashes once, then all the test cases that have to be executed after that will be affected because they are using the same driver, also you will be sharing cookies, and perhaps sessions already opened previously, etc.
The more robust solution is to create a new WebDriver for each test cases because doing that you are making all your tests cases less dependent on the others.
If the reason that is motivating you is the time each WebDriver takes to be created, perhaps you could start thinking on run test cases in parallel for example with TestNG.
Thanks
Best way to do that is, extend RemoteWebDriver and override startSession method-:
Steps:
Start selenium server using command- java -jar selenium-server-standalone-3.x.x.jar. By default your session start on port 4444.
open url http://localhost:4444/wd/hub/static/resource/hub.html
start new firefox session clicking on create session button and select firefox browser.
Once the session start, copy the session id and paste it in property file or xml file where you want.
read session id form the file where you saved in following method
#Override
protected void startSession(Capabilities desiredCapabilities) {
String sid = getSessionIDFromPropertyFile();
if (sid != null) {
setSessionId(sid);
try {
getCurrentUrl();
} catch (WebDriverException e) {
// session is not valid
sid = null;
}
}
if (sid == null) {
super.startSession(desiredCapabilities);
saveSessionIdToSomeStorage(getSessionId().toString());
}
}
You should instantiate your webdriver only once, when making a test and then pass it as argument for the other classes in constructors. Something like this:
public class Test {
WebDriver driver = new FirefoxDriver();
#Test
public void testHomePage() {
HomePage hp = new HomePage(driver);
//code here }
}
public class HomePage{
private static WebDriver driver;
public HomePage(WebDriver driver) {
this.driver = driver;}
}
In Java, when you say new a new object is instantiated. For WebDriver, every new is a new browser window.
If you want the same browser to be used then use the same driver object.
driver.get("URL PATH");
This will go to the new Url with the already open browser.
Java example.
First, you need to have Selenium server running.
java -jar C:\selenium-server-standalone-2.53.0.jar
To start a new session (first script):
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox());
Then, to reuse (attach) that session (second script):
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:7055/hub"),
DesiredCapabilities.firefox());
Notice the different port number.

Changing URL using WebDriver

I'm using Selenium WebDriver (Java) and am trying to change the URL after WebDriver has logged into a page.
Is there a way to either:
Change the URL of the current window, or
Open a new tab and go to another page.
Thanks!
You didn't share any code so i don't know how is your approach about it, I only share my knowledge about this subject.
1) For your first question i think you know how to open a new page with selenium web driver maybe you can use some wait method and then invoke the driver again.
//open browser
driver = new FirefoxDriver();
//login
driver.get("https://www.google.com/");
//set implicit wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Then invoke method again for your second request(I am not try this code maybe you need to create new driver object)
driver.get("https://www.stackoverflow.com");
2) For your second question this link help you.

Categories