Bringing a browser to focus in parallel execution selenium - javascript

Can we make sure one particular browser session to bring to front/focus in parallel execution of selenium?
I have a requirement to do keyboard action by pressing enter key , using robot class to handle google payment option in chrome.
And robot key actions are working only when the browser is in focus/front side.
Tried with ((JavascriptExecutor) driver).executeScript("window.focus();"); but no effect.
Also , tried with sikuli , and having the same issue with sikuli too, as it also needs browser to be in focus.
NOT going for an alternative to use Autoit , since our project needs to work on both Windows and MAC.
Your help is much appreciated.

You can send keys (Alt + Tab)to switch to the current session of browser since it was just the window in focus and then moved to state of being not focused on.
Here is the code sample in Java
Actions action = new Actions(driver);
action.sendKeys(Keys.chord(Keys.ALT, Keys.ENTER)).build().perform();

Related

ASP.NET: Exit application when press exit button

I am really a fresh guy for asp.net. I am using Visual Studio 2012.
I'm creating a login page, were there are two buttons Login and Exit.
When i click the Exit button then application have to be close and also stop the debugging.
My try: referred
I know there are other solutions on the given link for this problem but i prefer the following approach.
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
}
and written the following javascript function within the Login.aspx.
<script language="javascript" type="text/javascript">
function CloseWindow() {
window.close();
}
</script>
Note: The above script works fine if i click on exit button but application also get close when i click Login button and also debugging is not getting stop.
Your page is doing exactly what you asked it to do.
In your Page_Load, you called:
ClientScript.RegisterOnSubmitStatement()
That method causes every form submission to execute the script you provided; that means all of your buttons will run your CloseWindow procedure when they are clicked.
If you only want one of your buttons to close the window, then you should only attach the CloseWindow method to one of them. The answer you selected from the linked question only works because there's only one button on the form. I'd recommend you go with one of the other answers, e.g. using:
OnClientClick="javascript:window.close();"
as an attribute on your Exit button.
Handle the two buttons separately. Do whatever you want with the login button to submit the page and handle the postback but don't tie any events to the CloseWindow() function. Then, simply create and handle the Exit button like:
<input type="button" onclick="CloseWindow();" value="Exit"/>
The easy answer for your note is to use Internet Explorer as the default launch browser in the debug toolbar.
Unlike a winforms application, an ASP.Net application is stateless. The code that runs in the browser is not dependent on the same resources as the code that is running in the Visual Studio debugger. The only connection between them are the requests that the browser makes to the server (VS 2012 debugger either treats itself as a server or uses IISExpress) and the responses that the server sends back as part of those requests.
In most cases, this means that, when you close the browser, the server keeps on going, waiting for more requests. Internet Explorer works a little differently than the other browsers with Visual Studio. When the IE instance that Visual Studio launches gets closed, the debugger process also closes.
If you really are just starting out the ASP.Net, you should try the ASP.Net MVC framework. It has a cleaner separation between server and client side code, which may help you avoid some of these types of issues.

Selenium Webdriver Handling Popup window

I am trying to Automate a login scenario of my project.
After Hitting the URL a POPUP Authentication window comes even before the page loads.
If we not pass that window we can't see the home page.
the problem is once the pop-up comes i can't inspect the element using firebug,its not letting me to click anywhere else.
i tries to handle the window but still the control is not going to the username and password text box.
i tried windowhandle,robot class but not working.
Please anybody can help??
here is the piece of code:-
Set<String> handles = driver.getWindowHandles(); // get all window handles
System.out.println(handles.size());
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler);
also the robot class:-
Robot rb = new Robot();
//Enter user name by ctrl-v
StringSelection username = new StringSelection("myusername");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);
Thread.sleep(2000);
river.switchTo().window(mainHandle);
rb.keyPress(KeyEvent.VK_A);
rb.keyRelease(KeyEvent.VK_A);
rb.keyPress(KeyEvent.VK_M);
rb.keyRelease(KeyEvent.VK_M);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
//tab to password entry field
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
Thread.sleep(2000);
//Enter password by ctrl-v
StringSelection pass = new StringSelection("password");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pass, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
//press enter
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
//wait
Thread.sleep(5000);
You can do one thing. you can try it.
If we press ESC button then application stop processing many times.
after clicking on event use this below code:-
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
above code will press ESC for you. Now you can get the time to find the element.
Hope it will help you :)
Its not possible to automate the browser based authentication using Selenium.
If your authentication is http based then try opening the url using format - http://username:password#example.com/ instead of example.com.
If at all you still want to automate it then use third party plugins like AutoIt to accomplish it which works along with Selenium. AutoIt is used to automate windows based desktop applications including browsers. Here are few links that you can refer to -
Login to chrome using AutoIt
Handling Authentication using AutoIt
Use AutoIt with Selenium
Also you can use plugins for firefox which authenticates the popups automatically, when you open the url. But this is a manual setup and you have to perform authentication once so that the plugin remembers it. Here's one of them.
Hope this helps.

Disable JavaScript in Chrome with rendered page with C# & Selenium WebDriver

I am running my test in Visual Studio 10, with C# and Selenium. There is a moment that Im landing on a page and I need to check some elements of the page, but its like "loading" continuously so its impossible to find this elements, cause are changing. The only way to find them is in Chrome Settings, Disable JavaScript and perfect it works. But how I disable Javascript with selenium in the same page, without call a new one? Just refreshing or smthg. Thx!!!
ElBandido
You can set that to profiler. Handling would be different based on how you want to do it
var firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("javascript.enabled", false);
See this
For Chrome
DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability("chrome.switches", Arrays.asList("--disable-javascript"));
Taken from here

Clicking a Javascript link to make a post request in Python

I'm writing a webscraper/automation tool. This tool needs to use POST requests to submit form data. The final action uses this link:
<a id="linkSaveDestination" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("linkSaveDestination", "", true, "", "", false, true))'>Save URL on All Search Engines</a>
to submit data from this form:
<input name="sem_ad_group__destination_url" type="text" maxlength="1024" id="sem_ad_group__destination_url" class="TextValueStyle" style="width:800px;">
I've been using requests and BeautifulSoup. I understand that these libraries can't interact with Javascript, and people recommend Selenium. But as I understand it Selenium can't do POSTs. How can I handle this? Is it possible to do without opening an actual browser like Selenium does?
Yes. You can absolutely duplicate what the link is doing by just submitting a POST to the proper url (this is, in reality, eventually going to be the same thing that the javascript that fires when the link is clicked does).
You'll find the relevant section in the requests docs here: http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests
So, that'll look something like this for your particular case:
payload = {'sem_ad_group__destination_url': 'yourTextValueHere'}
r = requests.post("theActionUrlForTheFormHere", data=payload)
If you're having trouble figuring out what url it is actually be posted to, just monitor the network tab (in chrome dev tools) while you manually click the link yourself, you should be able to find the right request and pull any information off of that.
Good Luck!
With selenium you mimic the real-user interactions in a real browser - tell it to locate an input, write a text inside, click a button etc - high-level approach - you don't even need to know what is there under-the-hood, you see what a real user sees. The downside here is that there is a real browser involved which, at least, slows things down. You can though, automate a a headless browser (PhantomJS), or use a Xvfb virtual framebuffer if you don't have conditions to open up a browser with a UI. Example:
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('url here')
button = driver.find_element_by_id('linkSaveDestination')
button.click()
With requests+BeautifulSoup, you are going down to the bare metal - using browser developer tools you research/analyze what requests are made to a server and mimic them in your code. Sometimes the way a page is constructed and requests made are too complicated to automate, or there are anti-web-scraping technique used.
There are pros & cons about both approaches - which option to choose depends on many things.

Close the browser window in Vaadin

I have google'd quite a lot but was not able to find a solution for this problem. I have a Vaadin application that runs in a browser window. I have a logout button on it, clicking on it must invalidate the session and close the browser window. I was able to get this code to invalidate the session and close the application. However, I am looking to close the browser window also, which is where I am not having any success
WebApplicationContext webCtx = (WebApplicationContext) appRef.getMainWindow().getApplication().getContext();
HttpSession session = webCtx.getHttpSession();
session.invalidate();
appRef.getMainWindow().getApplication().close();
I am using vaadin 6.x and tried the following but they don't work on browsers I tried which is Chrome and IE.
appRef.getMainWindow().executeJavaScript("window.close();");
Any ideas would be greatly appreciated. Another question I have is do I need to get the name of the main Window and then call mainWindow.close(); or just window.close() ?
Read https://vaadin.com/book/vaadin6/-/page/application.close.html if you haven't done so far.
If appRef is already your Application, you do not have to call appRef.getMainWindow().getApplication() to get the Application. Just do appRef.close();
Do not invalidate the session manually. It messes with the vaadin lifecycle, so the next lines are not executed anymore, at least not in the vaadin context. Just do "application.close()" and let vaadin do the rest.
In vaadin 6 "window.close()" works, i use it with IE and chrome. So after you removed the session invalidation stuff, your code appRef.getMainWindow().executeJavascript("window.close()"); will work as expected.

Categories