I'm testing a website with validation etc. when the user types something incorrectly in each field and clicks save, a popup with the information appears. All those information are in the functions in the code (there is no JSON) file with them. My question is, how can I test them with selenium so when I'm writing a test script (in Java), so when the script is running they also show up on the console? I hope I explained my problem well.
Get Text From Pop Up:
String popUpText = driver.switchTo().alert().getText();
Accept Pop up Message:
driver.switchTo().alert().accept();
Related
I'm trying to compile the results of my class from my college's results page (exam.msrit.edu).
The USNs for my class are from 1MS16CS001-100
Is there any way that I could go about writing a scraper program to enter different values in the USN box and gather data?
I am quite new to scraping but have decent enough exposure to Python and java
Any advice is much appreciated :)
Not necessarily a scrape, but you can use Selenium Web Driver to browse to the page and input everything for you. Selenium Web Driver can be found here.
Essentially, once it is installed you only need to instantiate the driver and then loop through a list of values inputting them as you go.
from selenium import webdriver
# V sets up browser. If you want to use chrome addtional setup required
browser = webdriver.Firefox()
for i in len(100): #loops to arbitrary amount
browser.get("http://exam.msrit.edu/") #HTTP GET Request to page
elem = browser.find_element_by_id('id') #This is an html id. Could also use name, xpath, etc.
elem.send_keys("your string {}".format(i)) #sends up keys
elem. browser.find_element_by_id('id) #id for search button
elem.click() #clicks that element
The documentation on selenium is pretty good. http://selenium-python.readthedocs.io/navigating.html
This will open a actual browser and will take some time to load so it will not be the quickest way to do it but it will work. You can even take screenshots.
// opening the base URL
driver1.get(baseUrl+"/");
// opening a new tab
driver1.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND + "t");
driver1.get("my URL");
// getting back to the first tab
driver1.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND, Keys.SHIFT, "{");
// I want to signup by clicking the sign up button
driver1.findElement(By.xpath("/html/body/div[1]/header/div[2]/button")).click();
The error that I get after running is :
"Error communicating with the remote browser. It may have died."
But when I run the same code without the navigation, the button click works fine, then it means there is no problem with the xpath.
The information which browser you are using would be very interesting. And the webdriver doesn't need to control the visbile Tab. So i wouldn't let the Browser change the Tab and I would use the "switchTo" Method of the webdriver.
More information to this topic is here.
As #Kikkirej mentuined, i see no reason to use sendKeys to switch between opened tabs. Use Selenium instead, it is a much better approach.
Edit: in addition, try to provide more information, especially the most basic part - the browser you are automating.
I want to select the check box Prevent this page from creating additional dialogs before selecting ok to close the alert
Currently i am using
Alert alert=driver.switchTo().alert();
//check the checkbox
alert.accept();
As an normal interactive user to check the check box i have to use a combination of <Tab> + <Space/Enter> keys. The <Tab> shifts the focus to the check-box and the <Space/Enter> checks the check-box.
Solutions Tried
I tried using Java sendKeys mechanisms ( Robot class, driver.sendKeys(), etc.), but an UnhandledAlertException is getting thrown.
I tried using alert.sendKeys which is different from driver.sendKeys() but it too failed
//check the checkbox
alert.sendKeys("\t");
alert.sendKeys("{TAB}");
alert.sendKeys("\uE004");
alert.sendKeys("\\U+0009");
alert.sendKeys(Integer.toString(KeyEvent.VK_TAB));
I am trying to avoid robot class as much as possible as i need to run the test in grid in which case robot class will not work.Any pointers on how to send keys to the alert window ?
Temporarily i could do it using a javascript window.alert = function() {}; by simply overriding the alert with an empty function just curious to know if it could be done with webdriver functions like alert.sendkeys or any other methods ??
Any help is greatly appreciated!!
I'm creating screenshots in my testcases with selenium webdriver, and while these indeed show what is visible in my web application, it doesn't show popups created by the browser.
I have found that in IE in some cases, my app triggers a JS debug popup in IE. This is of course an error and breaks the rest of my test, but the screenshot does not show the error. I presume this is because it's an IE native popup, rather than one trigger by my application.
Is it possible to get this included in the screenshots? I was thinking of maybe creating the screenshot with Robot#createScreenCapture() but obviously that wouldn't show anything useful if the browser is minimized.
So, a few possible solutions:
- can you detect if an error message pops up in a browser
- is it possible to maximise/focus the browser while running?
- can you take screenshots from selenium with the popups showing?
Selenium will take a screenshot that represents the rendered DOM that the browser shows the end user by intercepting the rendered image that the browser is displaying and taking a copy of it.
It does not take a desktop screenshot so the screenshots shown will not show anything covering the browser window. JavaScript alerts are not part of the rendered DOM so you will not see these in Selenium screenshots.
Maybe because the driver is not with the Alert/Window active?
You could try something like this:
private void CheckForOtherWindows()
{
//Check for any other window open
if (driver.WindowHandles.Count > 0)
{
foreach (string window in driver.WindowHandles)
{
driver.SwitchTo().Window(window);
TakeScreenshot();
}
}
//Check for alert window
try
{
driver.SwitchTo().Alert();
TakeScreenshot();
}
catch
{
//Nothing
}
}
This is not tested, not sure if works. Just giving the idea. :)
Edit:
To maximize the window is easy:
driver.Manage().Window.Maximize();
Hope it helps.
You can use this as below:
FileUtils.copyFile(((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE), new File("E:\\ScreenShot\\screenshot.png"));
I want to record selenium script for Popup coming in the middle of website. I recorded the selenium script for the site, and when I ran it then in the middle of the processing I got advertisement popup, it is stopping my script execution.
How can I remove those popup or handle it using selenium?
If its an advertisement then I think it would be a new window, you can use getallwindowtitles, then loop thru the result to get to the adv window, use selectwindow and then close the window using close. Return back using selectwindow(0)
You can download and use Auto it. Following is the sample of the script that waits for a pop up title and then close it when appears.
While 1
$activeWindowTitle = WinGetTitle(WinActive(""));
If ($activeWindowTitle == "popuptitle") Then
WinClose("popuptitle");
EndIf
sleep(500)
WEnd