Custom alert box not loading in android WebView - javascript

In my application , I have a WebView to load a web app.The login is done using a alert box which takes username and password. when i try to load the URL in android browser the alert box appears like this. But when I try to load the same URL in my WebView it does not show any alert box .
After adding the following code
mwebView.setWebChromeClient(new WebChromeClient(){
#Override
public boolean onJsAlert(WebView view, String url, String message, android.webkit.JsResult result) {
Log.d("JSALERT", message);
new AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true).show();
result.confirm();
return true;
}
});
WebView shows simple alert box messages , but it does not show that custom alert box(the method is not getting fired).
In my app WebView , I wanted to show the login alert (which includes login fields and Buttons) .if any one knows the solution pls share with me
Thanks in advance

Well, what you see in Browser isn't actually an "alert box" -- it's an HTTP authentication dialog. In order to handle it, you need to override WebViewClient.onReceivedHttpAuthRequest, see docs.
You can check the relevant code in Android Browser here and here.

Related

Injecting JS into Literal and firing after page load

I have an ASPx/C# page that on clicking Save it will post back to the server. If the content within the controls already exists in the data stores which are checked it is supposed to pop-up an alert. The previous programmer used:
<asp:Literal ID="litError" runat="server" />
with the code behind ultimately sending:
litError.Text = "<script type='javascript'>alert('An error occured while processing this request. If the error persists, please contact the help desk.');</script>";
This JS alert is not popping up in spite of the debug reporting everything correctly processing through. I have scoured the internet, including here, for several days trying to find a resolution. I have tried many variations to get this to fire.
I'm suspecting that the script cannot fire on the AJAX because it is just not there during the Load stage of the life cycle, but would like some verification.
The script is in the btnSave_OnClick method. Unfortunately, due to the nature of the web application I cannot show more of the code, but the script should fire on exception of an item existing in either the app DB or if the user exists in our AD system already.
I was able to resolve my issue by completely removing the use of the ASP Literal control. I believe my first attempts at using this didn't have a properly formatted string, but it is now correctly functioning with the below code implemented in the code behind:
StringBuilder sbError = new StringBuilder();
sbError.Append("<script language='javascript' type='text/javascript'>alert('" + strError + "');</script>");
ScriptManager.RegisterStartupScript(this, this.GetType(), "sbError", sbError.ToString(), false);
Thanks to Zaigham and James for trying to help. I believe the reason the Literal control method did not work was because there was a ScriptManager control on the page(s) that were attempting to use that method.
I am not exactly sure why the script in literal is not working, but you can try this instead.
Your script text
string script = "<script>alert('Your alert message goes here');</script>";
Then use the method RegisterClientScriptBlock (in the same event where you previously set the error text) to inject the script to the page during the postback.
Page.ClientScript.RegisterClientScriptBlock(typeof(YOUR_PAGE_TYPE), "alert", script);
When the page is loaded back, you should see the alert popped up.
what I understand is; you want to run client script from code behind in the button click event (based on a condition). The best way to achieve this is to use ScriptManger class. Example:
Protected void btnSave_OnClick()
{
ScriptManager.RegisterStartupScript(btnSave, btnSave.GetType(), "myscript",
"alert('Your alert message goes here');", true);
}

How to handle Geo location popup using Selenium chromedriver?

When ever launching the web-app in chrome browser using selenium webdriver, Geo-location(Geo Location) Pop up is coming on the top of the browser.
Due to which some of my location based Test Cases are getting failed.
How to handle these kind of scenarios using chromedriver?
"When ever launching the web-app in chrome browser using selenium webdriver,
Geo-location(Geo Location) Pop up is coming on the top of the browser."
in this case, u may try something like this:
#BeforeMethod
public void clearPopup() {
driver.navigate().refresh();
Thread.sleep(2000);//wait until the page is loaded.
//try to use explicit wait
try {
Alert alert = driver.switchTo().alert();
alert.dismiss();
} catch (NoAlertPresentException ex) {
//System.out.println("No alert for this test case.");
}
}
this method will run before all ur testcase and if any popup shows it will dismiss the alert.
I also have go through ur url. when i click on call geolocation api, a pop is shown. U can stop this kind of pop up by doing the followings:
-go to settings of chrome and click on advanced settings.
-than click on content settings under privacy.
-than u will see pop ups and location options.
form there choose what option u want. Choose
"Allow all sites to track ur physical location"
if u don't want to see the popup.

Never Show Window On Page Launch

I am using C# and asp.net to launch a webpage that I am passing parameters to. That works well! I come from a Windows.Forms background so please forgive me if I am trying to achieve the impossible. What I would like is set the Visibility property of the program (either IE or chrome) to false so the user never sees that a webpage is being launched. I have been using this JS function to close the page, but it seems that the page must completely load before closing which sometimes can take a few seconds.
Does asp.net have the capability to achieve such? And this is my JS code I have been using
string close = #"<script type = 'text/javascript'>
window.returnValue = true;
window.close();
</script>";
base.Response.Write(close);
If you don't want the User to see the page, I assume you just want to post some information to the page. In that case, make an HTTP request via c# code, instead of opening the webpage up in a browser.
On the Project Properties page, Web tab, Start Action section, click the radio button for "Don't open a page. Wait for a request from an external application".

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.

javascript not working in android

I have a webview in my program.
I loaded a string into this webview that contains javascript function. When i loaded it in the emulator it's not working
i.e if I write a simple alert the webView won't display the alert.
I have enabled the javascript. But then also its not working.
what may be the reason?
Please help
Thankyou
Alerts will not normally work in a WebView. You will need to write the code to do that yourself. You can easily do that by implementing your own version of WebChromeClient. In otherwords:
class ChromeClient extends WebChromeClient {
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show();
}
}
...
mWebView.setWebChromeClient(new ChromeClient());
Another thing I would recommend is implementing the onConsoleMessage as well. This way you can just use "console.log" in your JavaScript and have it directed to Toast or the Android Log.

Categories