Opening a new browser tab - selenium

I've tried several functions but none seems to be working? For example:
element, _ := webdriver.FindElement(selenium.ByCSSSelector, "body")
element.SendKeys(selenium.ControlKey + "t")

Selenium is capable of executing javascript within the browser.
To open a new tab get selenium to run the following:
window.open()
I've not used Selenium & Go before - so I can't comment on the syntax. However it's normally along the lines of driver.ExecuteScript("window.open()"). See if your IDE will help you plug the gap.
After you get a new tab, you typically need to use the .switchTo in order to move selenium to another tab.
updated:
Docs suggest....
// ExecuteScript executes a script.
ExecuteScript(script string, args []interface{}) (interface{}, error)
see here

Related

Driver.getWindowHandles() is always returning 1 in IE11 on Windows 10

I know this question is duplicate with below question.
Driver.getWindowHandles() is always returning 1 in IE11 on Windows 10, although there are two windows open
Selenium - getWindowHandles() is returning value 1 irrespective of number of browser opened
But I research and try any solution but cannot resolve problem.
I also try setting follow guideline https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration
but it is not working.
This is my code for testing
WebDriver oldDriver = new InternetExplorerDriver(InternetExplorerDriverService.createDefaultService(), createIEOption(Server.OLD));
WebDriver newDriver = new InternetExplorerDriver(InternetExplorerDriverService.createDefaultService(), createIEOption(Server.NEW));
//waiting page load
TimeUnit.SECONDS.sleep(5);
while(true) {
//for test
TimeUnit.SECONDS.sleep(1);
System.out.println("OLD: " + oldDriver.getWindowHandles().size());
System.out.println("NEW: " + newDriver.getWindowHandles().size());
}
I open many tab but result getWindowHandles() always return 1.
The IE driver cannot connect to and control “manually” opened new tabs (i.e., those opened via Ctrl+t). There is no workaround. Do not attempt to use tabs with the IE driver.
Refer Selenium IE driver returns tab count as 1 while using getWindowHandles method on multiple tabs of same window

Protractor get console variable value

In Firefox browser developer tool console, I type var a = libraryform.firstname
It returns the firstname value entered by user for that form.
I am new to protractor and selenium. How can I call library.firstname in protractor to get the value of fistname?
Use Javascript Executor to execute the javascript which you able to get them working in developer console against your application.
For e.g.,
this returns web-element which retrieved through javascript executed in browser.
var element = browser.executeScript("document.getElementById('identifier1')");
Refer this page for more examples
If this single line returns required value in browser console, then the protractor code should be some thing like this,
var name = browser.executeScript('return libraryform.firstname')
If you need too many lines of js, you can specify with semicolon separated,
var name = browser.executeScript("var element=$('.dropdown-toggle').eq(0); return element.text();")

Not able to open new tab using selenium java in chrome latest version 65.0.3325.162

Below is my code for create new tab,open new window and switch to default window.
CreateNewTab:
jse.executeScript("window.open();");
Switch to OpenNewWindow:
deafultWindowName=driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);}
switchToDefaultContent
driver.switchTo().window(deafultWindowName);
I don't see any error message/issue with code but when I execute it all action gets performed in the main window only and it doesn't open new tab or new window. Please suggest!
Is it possible that you might be using an older syntax?
I usually use this:
((JavascriptExecutor)driver).executeScript("window.open('about:blank','_blank');");
//#Note: if you wanna hit a specific URL, Replace about:blank with you desired URL
You could also try any of the driver methods like:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");

Geb: Open new tab for each test

I am trying to open new tab for each iteration of the test for each set of data in the where block.
I am trying like:
setup:
Keys.chord(Keys.CONTROL, "t")
but it does not work.
How to do it?
I solved this problem by this [WRITING AT THE BEGGINING OF THE TEST]:
def cachedDriver = CachingDriverFactory.clearCacheAndQuitDriver()
Now a new window is opened and previous window is closed for every set of data in the where block and it is very helpful for executing thousands of tests.
To open a new window using WebDriver and therefore Geb as well you need to call the window.open() javascript method in the browser you're driving. Using Geb it can be done in the following way:
js.exec "window.open('about:blank', '', '')"

how to Pass command line argument ( baseURL) for selenium WebDriver

Kindly help.
i have created a runnable jar for my Selenium webDriver suite. now i have to test this in multiple environment( QA , Demo box, Dev ). But my manager doesnt what it to be hard coded like below
driver.get(baseUrl)
As the baseURl will change according to the need. My script is given to the build team. So all they will do in the command prompt is
java -jar myproject-1.0.1.jar
So my manager has asked me to send the baseUrl as a command line argument so that build team do not have to go to my script and manually change the baseUrl. They should be able to change the URL every time they run the script from the command line itself. Something like this
java -jar myproject-1.0.1.jar "http://10.68.14.248:8080/BDA/homePage.html"
Can somebody please guide me through this. Is it possible to send command line arguments to Selenium Web Driver driver.get(baseUrl)
Thanks in advance
From your question above I recon you want pass URL at runtime, means your URL changes time to time so beside hardcoded URL , you want pass at the time your automation code runs. So, let me give you 2 simple solutions.
You can send URL dynamically or at Run time by using javascript executor:
try{
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("var pr=prompt('Enter your URL please:',''); alert(pr);");
Thread.sleep(15000L);
String URL = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
driver.get(URL);
}catch(Throwable e)
{
System.out.println("failed");
}
Use this code in place of driver.get(); , so that a prompt box will appear when you run your code and within 15 secs or it will throw a error(you can change the time in Thread.Sleep) you will give the current Valid URL and hit Enter, the navigation will go to the URL. So that you can use different URL for same set of testscripts.
By using Scanner Class:
String url = "";
System.out.println("Enter the URL :");
Scanner s = new Scanner(System.in);
url = s.next();
s.close();
By using this you can give needed URL in your Console (If you are using Eclipse).
I recommend try Javascript excutor in your code and then create a runnable jar file and just run the Jar file, you will know and it will be the better solution than commandline URL passing . Let me know :)
Another way is to supply arguments this way -Durl=foobar.com and extract them in runtime like this
String URL= System.getProperty("url")