Webdriver to open a mail in Gmail

18,459

Solution 1

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

Solution 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.

Solution 3

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.

Solution 4

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!

Share:
18,459
ChrisOdney
Author by

ChrisOdney

A weary weary Software Developer

Updated on June 04, 2022

Comments

  • ChrisOdney
    ChrisOdney about 2 years

    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.