3

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.

ChrisOdney
  • 6,066
  • 10
  • 38
  • 48
  • Please scroll down and follow the answer with most votes and not the once accepted as correct answer. – Tarun Jul 19 '16 at 08:06

9 Answers9

10

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

nilesh
  • 14,131
  • 7
  • 65
  • 79
  • 2
    +1 If you're not testing the Gmail interface, but instead just testing a mail process (receiving or sending) then another solution will be much more preferable. Google constantly change the UI implementation to prevent scripting, so your automated tests will break every time that happens. Use a service over which you have 100% control. – Dan Blows May 21 '12 at 09:35
  • 1
    If i could upvote this over and over again, I would. Everyone, please, for the love of all that's holy, do **NOT** automate GMail's UI using Selenium or WebDriver. – JimEvans May 08 '13 at 18:47
2

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.

1

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!

1

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.

9ikhan
  • 1,177
  • 3
  • 11
  • 22
  • I tried the code below and it is not working for me. I am trying to open an email in the gmail inbox. WebDriver driver = new FirefoxDriver(); loginToGmail(driver); WebElement mailLink = driver.findElement(By.xpath("//div[5]/div/div/table/tbody/tr[1]")); System.out.println("mailLink.getText(): "+mailLink.getText()); – ChrisOdney May 04 '11 at 06:08
  • this link is for opening the mail. use mailLink.Click(). Also, it is quite possible that gmail is rendered differently in your country than mine, in that case xpaths will be different. Try it using selenium IDE first, if it works then put it in your code. – 9ikhan May 04 '11 at 06:25
  • It is not able to locate the element, so no point in invoking the click method. I tried using the Selenium IDE, I recorded my actions and clicked on a mail. But the IDE did not give anything for the target node. I noticed that the id of the table is :p3 and tried getting and handle to it using : //table[@id=':p3'] . Did not work either. – ChrisOdney May 04 '11 at 07:41
  • Not all the elements are clickable.Use ClickAt and Target should xpath of tr inside of tbody of the table. Currently you are navigating till table, please look into the child element of this tag, this will have the tr- which are rows of mail in inbox. – 9ikhan May 04 '11 at 08:44
  • I said, I am not able to locate the table element. So invoking any method on it or retrieving its child elements is out of question. – ChrisOdney May 04 '11 at 11:53
  • table that i've mentioned is same as the one written in your comment, i.e table[@id=':p3']. I suppose you have copied that xpath using firebug. somehow, it is not giving the full xpath for these elements. Inside this table there is a tbody tag, copy the xpath of that using firebug and just add "/tr[n]" to it. CLickAt on that target will work. – 9ikhan May 04 '11 at 12:09
  • did you look at the question: http://stackoverflow.com/questions/1737957/using-selenium-in-order-to-read-emails-on-gmail – Guy May 04 '11 at 14:50
  • @9ikhan Will try that and let you know. Don't have access to the code right now. @Guy That is very helpful, will try that out in the morning and get back. – ChrisOdney May 04 '11 at 16:55
  • `code` WebDriver driver = new FirefoxDriver(); loginToGmail(driver); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver = driver.switchTo().frame("canvas_frame"); WebElement mailElement = driver.findElement(By.xpath("//div[@class = 'y6']/span[contains(.,'subject')]")); mailElement.click(); `code` Thanks @Guy and 9iKhan, above is the code that worked for me. – ChrisOdney May 05 '11 at 09:24
0

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.

josliber
  • 43,891
  • 12
  • 98
  • 133
MCKiran
  • 57
  • 3
  • 9
0

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");
spenibus
  • 4,339
  • 11
  • 26
  • 35
Arian Al Lami
  • 867
  • 7
  • 9
0

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()
0

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.

Nikhil
  • 16,194
  • 20
  • 64
  • 81
-1

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

Ryan K
  • 3,985
  • 4
  • 39
  • 42