I'm having trouble getting through basic authentication on one of our websites on IE8 using Watir.
When I navigate(goto) the url, the basic authentication dialog is displayed showing 'Connect to ' as the title, with user name, password edit fields and OK, Cancel buttons (the usual!).
In Watir, after passing the following code:
b = Watir::Browser.new
b.goto '<our url>'
the dialog is displayed but the goto statement never completes. If I cancel or login manually, then the goto statement returns a number (this is in IRB by the way).
I've done lots of investigation and tried out all sorts of ideas but now seem to be stumped!
When we login to the site manually, we have to pass our credentials like below:
User Name: <domain>\<username>
Password: <password>
I've tried this in the url string i.e.
http://<domain>\<username>:<password>#<url>:<port>
http://lnd\mbartram:mypassword#xxx.xx.xx.xx:xxxx
but this doesn't work in code or if I enter it manually in the address bar.
Perhaps I need to escape the \ part to get this to work but how?
Any help is greatly appreciated.
M.
Ok, using a bit of a hack but it works consistently for me. I'm using a simple Wscript.Shell SendKeys method. See below:
`start #{theURL]`
loginDialog = WIN32OLE.new('Wscript.Shell')
loginDialog.AppActivate('Connect to #{ip}')
sendKeys = "#{userName}{TAB}"
loginDialog.SendKeys(sendKeys)
sleep(1)
sendKeys = "#{password}{TAB}{ENTER}"
loginDialog.SendKeys(sendKeys)
Related
edit: My issue was never quite resolved, but the answer below provided an interesting result.
The app will eventually be called by a powershell script and in this situation, I don't have the issue explained below. No additional {ENTER} is required in the PS console, so no more issue! I couldn't care less if an extra ENTER is required when my app is launched manually via cmd.exe
Problem: In a VB.NET
form app, I'm unable to get the console back to it's "default" state after the code is finished running. I need to press enter manually.
My app can also be executed from command line (in this case, no form is opened. Code is being executed automatically and output sent to console for user to see what happens)
I call AttachConsole(-1), run some code, and when everything's finished I see my latest message in console, but it's as if the process wasn't quite finished.
I have tried SendKeys.SendWait("{ENTER}"). It works well, but only when the console is the current focus. If I click outside the console while the code is running, the ENTER key is sent to whichever window I made active.
So I tried to make the console the current window:
Dim bProcess As Process = Process.GetProcessesByName("cmd").FirstOrDefault()
SetForegroundWindow(bProcess.MainWindowHandle)
// I also tried AppActivate(bProcess.Id)
SendKeys.SendWait("{ENTER}")
FreeConsole()
Nope, the ENTER key will still be sent somewhere else and not to the console. But it does make the console blink orange, so SetForegroundWindow seems to do something...
Any help will be greatly appreciated :)
EDIT
In response to #TnTinMn's answer:
FreeConsole()
// SendKeys.SendWait("test")
PieceOfCodeFromTnTinMn()
Same behavior as I've had so far: This will send the SendKeys.SendWait("~")command "outside" the console if it loses focus while the code is running.
BUT with the 2nd line above un-commented, "test" is sent outside the console and SendKeys.SendWait("~") is sent to the console, as expected.
I'm still trying to figure out what is happening here...
You can use the VB Interaction.AppActivate Method to activate the parent console prior to calling SendKeys.SendWait. This requires that you obtain the ProcessID of the console window that is the parent process of your application.
One way to do this is using Windows Management Instrumentation (WMI). The following is not pretty, but it appears to work. You would execute this after calling FreeConsole
Using currentProcess As Process = Process.GetCurrentProcess
Dim query As New SelectQuery()
Dim props As New StringCollection
props.Add("ProcessId")
props.Add("ParentProcessId")
With query
.ClassName = "Win32_Process"
.Condition = $"ProcessId={currentProcess.Id}"
.SelectedProperties = props
End With
Dim parentProcessId As Int32
Using searcher As New ManagementObjectSearcher(query)
Using mos As ManagementObjectCollection = searcher.Get
Using en As ManagementObjectCollection.ManagementObjectEnumerator = mos.GetEnumerator
If en.MoveNext() Then
parentProcessId = CInt(en.Current.Item("ParentProcessId"))
End If
End Using 'en
End Using ' mos
End Using 'searcher
If parentProcessId <> 0 Then
AppActivate(parentProcessId)
SendKeys.SendWait("~")
End If
End Using 'currentProcess
I'm implementing an invisible reCAPTCHA as per the instructions in the documentation: reCAPTCHA V2 documentation
I've managed to implement it without any problems. But, what I'd like to know is whether I can simulate being a robot for testing purposes?
Is there a way to force the reCAPTCHA to respond as if it thought I was a robot?
Thanks in advance for any assistance.
In the Dev Tools, open Settings, then Devices, add a custom device with any name and user agent equal to Googlebot/2.1.
Finally, in Device Mode, at the left of the top bar, choose the device (the default is Responsive).
You can test the captcha in https://www.google.com/recaptcha/api2/demo?invisible=true
(This is a demo of the Invisible Recaptcha. You can remove the url invisible parameter to test with the captcha button)
You can use a Chrome Plugin like Modify Headers and Add a user-agent like Googlebot/2.1 (+http://www.google.com/bot.html).
For Firefox, if you don't want to install any add-ons, you can easily manually change the user agent :
Enter about:config into the URL box and hit return;
Search for “useragent” (one word), just to check what is already there;
Create a new string (right-click somewhere in the window) titled (i.e. new
preference) “general.useragent.override”, and with string value
"Googlebot/2.1" (or any other you want to test with).
I tried this with Recaptcha v3, and it indeed returns a score of 0.1
And don't forget to remove this line from about:config when done testing !
I found this method here (it is an Apple OS article, but the Firefox method also works for Windows) : http://osxdaily.com/2013/01/16/change-user-agent-chrome-safari-firefox/
I find that if you click on the reCaptcha logo rather than the text box, it tends to fail.
This is because bots detect clickable hitboxes, and since the checkbox is an image, as well as the "I'm not a robot" text, and bots can't process images as text properly, but they CAN process clickable hitboxes, which the reCaptcha tells them to click, it just doesn't tell them where.
Click as far away from the checkbox as possible while keeping your mouse cursor in the reCaptcha. You will then most likely fail it. ( it will just bring up the thing where you have to identify the pictures).
The pictures are on there because like I said, bots can't process images and recognize things like cars.
yes it is possible to force fail a recaptcha v2 for testing purposes.
there are two ways to do that
First way :
you need to have firefox browser for that just make a simple form request
and then wait for response and after getting response click on refresh button firefox will prompt a box saying that " To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier. " then click on "resend"
by doing this browser will send previous " g-recaptcha-response " key and this will fail your recaptcha.
Second way
you can make any simple post request by any application like in linux you can use curl to make post request.
just make sure that you specify all your form filed and also header for request and most important thing POST one field name as " g-recaptcha-response " and give any random value to this field
Just completing the answer of Rafael, follow how to use the plugin
None of proposed answers worked for me. I just wrote a simple Node.js script which opens a browser window with a page. ReCaptcha detects automated browser and shows the challenge. The script is below:
const puppeteer = require('puppeteer');
let testReCaptcha = async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('http://yourpage.com');
};
testReCaptcha();
Don't forget to install puppeteer by running npm i puppeteer and change yourpage.com to your page address
I'm using VBscript to use IE to open a webpage - login - and save some data to a file.
Why use IE?
- I can use VBScript to interact with the webpage
- The webpage uses ajax to do verification and signing in, so simply using MSXML2.XMLHTTP wont work.
After IE log into the site, I direct it to the page I want to save as a file, the problem is this page is returning JSON data (filename.json) - so IE is prompting to open/save/saveas rather than just opening the page, so I cant save the data of the page.
The code is below. Any help is much appreciated.
Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")
IE.Visible = True
IE.Navigate "https://www.somesite.com"
Wait IE
With IE.Document
.getElementByID("username").value = "myusername"
.getElementByID("password").value = "mysecret"
.getElementsByName("Login")(0).Click
End With
Wait IE
'# all fine here, logged in and redirected to members page
IE.Navigate "https://www.somesite.com/api/data"
' # this link returns json data
'# this is where everything stops because Im getting a download/save as prompt in IE
EDIT: ok, I was able to get IE to display the content rather than downloading it by editing the registry to treat .json file as text files,Now the problem is the page is a json file, not a HTML file, so I cant get it's content by calling IE.Document.Body.InnerHTML like normal HTML pages.
How would I get the content of the page and assign it to a variable so I can process it?
oh, I just put this in my code:
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite "HKEY_CLASSES_ROOT\.json\Content Type", "text/plain", "REG_SZ
That way the file is treated as a textfile within IE and it will just open in the page. Then I can use the normal objIE.Document.Body.InnerText to get the json contents. Then I pass that variable onto my function that decodes it all.
Hope this helps someone, because all the results on google I found did not help me. Only reason I posting on this old thread!
Cheers,
Ross
on my web application, after clicking a download button, a popup message with content "Do you want to open or save "abc.txt" from this site?" with 3 buttons ("Open", "Save" & "Cancel") will be displayed at the bottom of page.
I'm trying to close/quit this browser session with below codes:
#driver.execute_script "window.onbeforeunload = function(e){};"
#driver.quit
However, the browser is NOT (but should be) closed. I'm working with Selenium Ruby Webdriver. Please guide me a way to resolve this problem. Thanks so much.
Note that with the above codes, I'm able to close IE9 browser that has the popup message "This page is asking you to confirm that you want to leave - data you have entered many not be saved" with "Leave Page" and "Stay on Page" buttons successfully. But, codes do NOT work in case the popup message with content "Do you want to open or save "abc.txt" from this site?" with 3 buttons ("Open", "Save" & "Cancel") displayed.
WebDriver has no control over these types of "Save file" dialog prompts.
Please peruse this article on this subject (note, his examples are in Java, but they can all be ported easily and with less code in Ruby). http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/
Thus, I would recommend not even clicking the link in question and trying to deal with the dialog. Instead, grab the href value and initiate a Net::HTTP request to it like this SO response shows you how to do: https://stackoverflow.com/a/4581116/1221475 . You can then check the file for correct contents and such using standard Ruby and file parsers.
This is a hack by Dave Haefner. It is written for Java/Selenium combination, but you can easily convert it to Ruby syntax.
If you don't care if a file was downloaded or not and you want to confirm only that a file can be downloaded, you can use an HTTP request. Instead of downloading the file you'll receive the header information for the file which contains things like the content type and length. With this information, you can confirm the file is you expect.
String link = driver.findElement(By.cssSelector("download-link-element")).getAttribute("href");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpHead request = new HttpHead(link);
HttpResponse response = httpClient.execute(request);
String contentType = response.getFirstHeader("Content-Type").getValue();
int contentLength = Integer.parseInt(response.getFirstHeader("Content-Length").getValue());
assertThat(contentType, is("application/octet-stream"));
assertThat(contentLength, is(not(0)));
I'm writing some watir test cases:
browser.goto "http://egauge2592.egaug.es/"
browser.link(:href,"/settings.html").click
browser.text_field(:index,4).set("some text")
browser.button(:id,"save_button").click
then a 'Authentication Required' dialogue opens, asking for username and password .
No matter how I tried, I couldn't access the text fields.
I tried send_keys and JavaScript.
I also tried Watir.autoit but it says undefined method.
I'm using watir on a Ubuntu machine with the FireFox browser.
How to fill in username and password fields of that dialogue box?
I was able to enter username with browser.alert.set but could only set username couldn't access password field.
I wrote a firefox plugin to deal with this problem recently. I haven't tried it with headless Firefox, but it might work... worth a try. See the following for details:
http://www.natontesting.com/2012/10/06/firefox-plugin-test-automation-password-manager/
To get it working with watir, try the following:
browser = Watir::Browser.new
#go to a page that won't bring up the authentication dialog...
browser.goto 'http://www.google.co.uk'
#prepare the script...
pass_man_update_script = <<-SCRIPT
var addCredentialsEvent = new CustomEvent("add_credentials_to_passman", {
detail: {
host: 'http://secure.example.com',
username: 'bob',
password: 'P45sword'
}
});
window.dispatchEvent(addCredentialsEvent);
SCRIPT
#inject the script into the browser:
browser.execute_script pass_man_update_script
#go to the page that prompts the dialog box...
browser.goto "http://secure.example.com"
#you should now be past the dialog box!
I use watir-webdriver instead and this (http://watirwebdriver.com/basic-browser-authentication/) works for me.
Just put your basic auth credentials in the url:
browser.goto "http://user:pass#egauge2592.egaug.es/"