Testing Auto-Complete Combo box in Selenium - selenium

Has anyone a good solution on how to test auto-complete combo box in Selenium?
Thanks for the help,
Manjide

Take a look at my other answer Selenium - verify the list of suggestions is displayed

Using TestPlan with the Selenium back-end this code grabs the suggestions from Google -- which is an example of a auto-complete combo box.
GotoURL http://www.google.com/webhp?hl=en
ClickReplaceType //input[#name='q'] what is my
# This is where the suggestions appear
set %ResultsXPath% //table[#class='gac_m']//td[#class='gac_c']
# Check that they are there (that is, wiat for them, since they are dynamic)
Check %ResultsXPath%
# Then iterate over the suggestions
foreach %Cell% in (response %ResultsXPath%)
Notice %Cell%
end
This produces the results:
00000000-00 GOTOURL http://www.google.com/webhp?hl=en
00000001-00 NOTICE Starting a new browser (0:0:0:0:1) com.thoughtworks.selenium.DefaultSelenium#332611a7
00000002-00 NOTICE what is my ip
00000003-00 NOTICE what is my ip address
00000004-00 NOTICE what is my bmi
00000005-00 NOTICE what is my house worth
00000006-00 NOTICE what is my
Usually such tests work in both the Selenium and HTMLUnit backend in TestPlan, but Google currently works with Selenium.

Related

Unable to locate element using Selenium

I have reviewed several questions pertaining to this popular topic but have not yet found a solution. I am trying to scrape a dynamic webpage that requires the user to click something and then enter some input. The site I am trying to scrape is here: https://a810-dobnow.nyc.gov/publish/#!/
I am trying to click where it says "Building Identification Number" and proceed to enter some input. I cannot seem to even locate the element I need to click. I used a wait and also checked to see if it was located in some other frame I needed to switch to, it is not as far as I can see:
driver = webdriver.Chrome("C:\\Users\\#####\\Downloads\\chromedriver_win32\\chromedriver.exe")
driver.get("https://a810-dobnow.nyc.gov/publish/#!/")
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[#id=""accordiongroup-9-9180-tab""]/h4/a/span/h3")))
driver.find_element_by_xpath("//*[#id=""accordiongroup-9-9180-tab""]/h4/a/span/h3").click()
I just loaded the page, and when i try to search the dom for the xpath you have provided it fails to find the matching element.
I'd recommend using something like:
driver.find_element_by_xpath("//h3[contains(text(), 'Building Identification Number (BIN)')]").click()
Hope this helps

How to click on each marker of google map present on any website using selenium webdriver

How to do the Automation testing of any Google map. I have a map in my project/application, now I want to click on each markers.
Since you have no experience with WebDriver, I'm going to give you the answer you want (and not the one you need which is "Go look at WebDriver manual and tutorials.").
Java example:
// opens up Chrome, but you can use any other browser
WebDriver driver = new ChromeDriver();
// goes to GMaps page and searches for "Washington"
driver.get("https://maps.google.com/maps?q=Washington");
// clicks the only marker on the page
driver.findElement(By.cssSelector("img[src*='markerTransparent.png']")).click();
// don't forget to kill the browser or else you'll have neverending chromedriver.exe processes
driver.quit();
Now, you need to take a step back, look at WebDriver, choose a language in which you want to write your tests, go through the API and some examples, then try to implement your tests and if something goes astray, feel free to post another question with a particular issue (just make sure to search for it first).
You can click on each marker by locating that marker using ID.
Here is one script which I wrote to click on marker of google maps
d = Selenium::WebDriver.for :firefox
d.get 'http://maps.google.com'
d.find_element(:id, 'gbqfq').click
d.find_element(:id, 'gbqfq').send_keys 'hdfc bank pune'
d.find_element(:id, 'gbqfb').click
d.find_element(:id, 'mtgt_J.1000').click

Scrapy shell XPATH not working

I tried the following XPATH in XPATHHelper in Chrome and XPather in Firefox and it always displays all the snippets(ie the description of the search results) in google search result page, but it does not work in the Scrapy shell:
//span[#class='st']
In case it matters, I invoke scrapy shell like this:
scrapy shell "http://www.google.com/search?q=myQuery"
and I say hxs.select("//span[#class='st']"). This always returns an empty list.
Any clues as to why this could be happening?
Scrapy is not able to "parse" sites that need Javascript execution. What different developer consoles show you is the already interpreted and executed site with all Javascripts applied.
Since Google displays its resulst with the help of Javascript, the Scrapy on its own can't handle this.
sometimes sites will not work with Javascript Disabled (Applebees.com for example) so you have to use an actual browser like Selenium.
In Firefox url bar type :
about:config
find the line javascript.enable and change its value to false
Install FireFinder extension
Open Firebug (F12)
and then enjoy scraping google like xpath expression :
//*[#id="search"]//li[#class="g"]/div[#class="s"]//cite

Selenium not able to load dynamic options in a dropdown

Options for a dropdown on a webpage I am testing are dependent upon the values supplied for earlier textboxes and selects (E.g. based on the currency and amount specified, dropdown for product will show appropriate values. With no values, the supplied the dropdown is blank.).
Now, although I have provided the values for currency and amount, the product dropdown is still blank. It is not fetching the filtered values based on earlier data supplied. I am using Selenium server (2.24.1) and writing scripts in Java in Eclipse with TestNG and testing on IE8.
When inspected, the dropdown is no different from others, only its options change based on the values of other elements on the page. The web application is developed in Java (Wicket framework).
The Selenium code:
selenium.select(ownerBranch, "label=4521 - Branch one");
selenium.select(currency, "label=SEK - Swedish kronor");
Thread.sleep(sleep);
selenium.type(amountSantioned,"100000");
Thread.sleep(sleep);
selenium.click(chooseLoanTermBymatDate);
Thread.sleep(sleep);
timeNow=Calendar.getInstance();
timeNow.add(Calendar.DATE,+360);selenium.type(maturityDate,dateformat.format(timeNow.getTime()));
Thread.sleep(sleep);
selenium.type(amountSantioned,"100000");
Thread.sleep(sleep);
selenium.select(serviceDelChannel, "label=BackOffice");
Thread.sleep(sleep);
selenium.select(product, "label=");
Thread.sleep(sleep*2);
selenium.select(product,"label=LN7292 - Consumer loan for Year2026");
Thread.sleep(sleep);
I'm not going to try to reproduce the issue (if you can point me to a publicly visible site with similar behaviour, I might test it), so I'm only taking a guess here:
Since Selenium RC is written in pure Javascript and "only" firing change events on selecting values from drop-downs, Wicket is probably waiting for something else or relying on a completely different mechanism.
Things you can try:
Use Selenium WebDriver. Selenium RC has been deprecated for over a year now, because it had serious technical limitations (you might have just bumped into one) that are now solved by WebDriver. Also, you won't ever have to use Thread.sleep() again (although I'm almost sure it could be got rid of even here, mostly). This solution is the most painful, but is almost guaranteed to work well, because WebDriver behaves like a real user.
Call selenium.fireEvent() on all the input elements you're interacting with. Useful events might be focus, blur, maybe even click in between them.
Calling selenium.keyPressNative(String.valueOf(KeyEvent.VK_ENTER)) (presses Enter natively) after you every change of a dropdown. If the changed dropdown is not focused before this, you might need to focus() it beforehand.
The painful way that tries to simulate user's behaviour as close as possible instead of using JS methods: Instead of using select(), try to focus() a dropdown element, then select one of its options by pressing Down arrow repeatedly, then Enter.

Webdriver to open a mail in Gmail

I have started using Webdriver to automate our testing which is 100% manual.
In one of the use cases I need to click on a link sent to the user's inbox. I am facing problems with Gmail in this case. After logging in I am not able to figure out how to open a particular email.
I know the email subject etc but I am unable use it to access the element. Gmail receives all its data as JSON and then build the entire page through js functions. So the webdriver is not able to access any of the elements built using the JSOn data received.
Any help is greatly appreciated.
Thanks,
Chris.
I would suggest NOT to use UI to verify Gmail. Gmail's UI is extremely complicated and it's a trap. To me, automating with selenium is not a solution at all.
Consider using JavaMail API and HTTPURLConnection. This is what I do in a similar testing scenario.
While running the webdriver test, after doing certain action if you expect an email then using JavaMail API poll (for a certain timeout if its not immediate) for the email in the background with certain 'subject' or 'sender' etc. Once the email is found then grab the link from the email content and then simulate a click using HTTPURLConnection
If you can search the specific email you can use the following code to locate the email you are looking for:
//div [#class='y6']/span[contains(.,'<your original search text>')]
mind that google will cut off the subject which results in something like 'subject...' if the subject is too long. We use a unique number to identify messages in our automated test environment.
I don't think I got your question correctly but I suppose you are having trouble finding the locator to open the mail after you've entered text in search box of gmail.
//div[5]/div/div/table/tbody/tr[n]" //n is the row no. of mailbox, for first result use 1 and like
use this as identifier for element before cliking on it.
Hope this helps.
In my case, I found the solution by using Action class of Web driver
Pre-requisite: Your driver needs to move to specific frame to locate element
wd.switchTo().frame("canvas_frame");
Step 1) Search for specific email that is created/generated using below code
String searchvalue="html/body/div[1]/div[2]/div/div[1]/div[3]/div/div[1]/div[2]/div[2]/div/form/fieldset[2]/div/div/div[2]/input";
wd.findElement(By.xpath(searchvalue)).sendKeys(sendkeys);
String clickSearch=".//*[#id='gbqfb']";
wd.findElement(By.xpath(clickSearch)).click();
Step 2) Now use Actions class to navigate through.
Actions action= new Actions(wd);
action.click(firstrecord).build().perform();
Hope this helps out!
The above answer is correct to identify a mail in Gmail if we replace the subject.
I have tried with Selenium IDE to find the object with the
target as
xpath=//div [#class='y6']/span[contains(.,'<your original search text>')]
The object was found but click is not opening the mail.
So after some investigation I found that mouseDown method is working with the above xpath to open a mail.
So command will be
selenium.mouseDown("xpath=//div [#class='y6']/span[contains(.,'<your original search text>')]");
or
<td>mouseDown</td>
<td>xpath=//div[#class='y6']/span[contains(.,'£10 OFF when you spend £30 or more online')]</td>
<td></td>
in the IDE.
Hope this helps.
The current locator for gmail body is:
driver.findElement(By.className("LW-avf")).click();
driver.findElement(By.className("LW-avf")).clear();
driver.findElement(By.className("LW-avf")).sendKeys("your body message");
The below selenese command would do:
clickAt | //table/tbody/tr/td[5]/div[#class='yW'] |
Click at the FROM field of first/recent/top most mail to go to mail detail page. // note: tr for first mail, tr[2] for second and so on.
Try this out , working perfectly for me. This will select random emails, you can also modify as your requirement
for i in xrange(int(num)):
time.sleep(3)
m=random.randint(1,10)
print("Mail Number "+str(m)+" is selected")
browser.find_element_by_xpath("//div[#role='tabpanel'][1]//table//tr"+str([m])).click()
time.sleep(3)
browser.find_element_by_xpath('//*[#id=":5"]/div[2]/div[1]/div/div[1]/div/div/div').click()
You can use this also if you want to open a particular mail in gmail:
driver.findElement(By.xpath("//tr[i[td[4[div[contains(#class,'yW')]]]")).click();
Here i is the mail number which you want