How to map xpath of web element in its actual class? - selenium

I am creating a framework in selenium from scratch where i am scripting below scenarios:
1.Login in to https://www.yahoomail.com
2.entering username
3.entering password
4.click on Sign-In
5.click on Compose button
6.Enter Email ID, Subject and Message body.
Below is the code script i have written for above scenario:
WebDriver oYahoo = new FirefoxDriver();
oYahoo.get("http://www.yahoomail.com/");
oYahoo.manage().window().maximize();
oYahoo.findElement(By.xpath(".//*[#id='login-username']")).sendKeys("abcdefasdf#yahoo.com");
oYahoo.findElement(By.xpath(".//*[#id='login-passwd']")).sendKeys("sfgas234#123");
oYahoo.findElement(By.xpath(".//*[#id='login-signin']")).click();
oYahoo.findElement(By.xpath(".//*[#id='Compose']/button")).click();
oYahoo.findElement(By.xpath(".//*[#id='yui_3_16_0_1_1448364357109_2222']")).sendKeys("abcdefgh#gmail.com");
oYahoo.findElement(By.xpath(".//*[#id='subject-field']")).sendKeys("Hi This is my first automated mail");
oYahoo.findElement(By.xpath(".//*[#id='yui_3_16_0_1_1448364357109_1966']")).sendKeys("Hi This is my first automated mail");
oYahoo.findElement(By.xpath(".//*[#id='yui_3_16_0_1_1448364357109_2465']")).click();
oYahoo.quit();
Scripts fine till it clicks on "Compose" button, Once i get Mail editor, Script does not enters email-ID,Subject, and Message body.
What other action i should perform to achieve the same so that script will enter these parameters and can send a mail to particular user.
Do we need to create some class which will maps the locators to "compose-Email" screen?
If yes, how we can map/assign x-path to particular web element of Compose-Email page.
Thanks in Advance.

Well you should try by using the xpath and enter the required text. Something like this (it works for me)-
oYahoo.findElement(By.xpath(".//*[#id='to-field']")).sendKeys("xxxx#xxx.com");
oYahoo.findElement(By.xpath(".//*[#id='subject-field']")).sendKeys("My first automated email");
oYahoo.findElement(By.id("rtetext")).sendKeys("Hello....Hi....This is my first automated email");

Related

how to automate "amazon-add-to-cart" procedure using java selenium without opening the browser

I want to automate "amazon-add-to-cart" procedure.
the flow is to send http get method of the requested items, and then
submitting a button of the returning html.
the 1st url for example is:
https://www.amazon.com/gp/aws/cart/add.html?AssociateTag=your-tag&ASIN.1=B003IXYJYO&Quantity.1=2&ASIN.2=B0002KR8J4&Quantity.2=1&ASIN.3=B0002ZP18E&Quantity.3=1&ASIN.4=B0002ZP3ZA&Quantity.4=2&ASIN.5=B004J2JG6O&Quantity.5=1%22
after that the user should click the continue button in order to indeed adding the items to the cart.
can you provide an example of how to code this flow with the button click using java selenium - without openning the browser?
thanks.
use the "HTMLUnitDriver" Webdriver
WebDriver driver = new HtmlUnitDriver();
driver.get("Enter the URL");

how to pause selenium web driver for user action

I want to automate an application where user has to enter the OTP at a particular interval . Is it possible to automate is such a way using Selenium?
Ideally there should be no Manual Intervention when Automated Test Execution is In Pogress.
If entering an OTP is one of the step of the Usecase it should be automated too.
Note : OTP can be sent to an email address and can be validated.
If your are running the tests in Dev environment, One possible solution would be to request the developers to set a constant value for the OTP and then validate the field with the value.
More complex solution would be to get the value from message box which will include appium concepts or to read the OTP value from the mail box
You don't have to handle this with Selenium, rely on programming language. Display a box/message, when user enters pass that to the form via selenium send keys and continue your automation. Selenium will actually wait unless your page expires. Below code can help.
driver.findElement(By.id("username")).sendKeys("username");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String otp;
System.out.println("Please Enter the OTP Code:: ");
otp = br.readLine();
System.out.println("Entered OTP Code is:: "+otp);
driver.findElement(By.id("password")).sendKeys("otp");
Suppose you want to enter your OTP at this page,
www.your-page.com/OTP
once this page spawns, use new Scanner(System.in).nextLine
to manually wait for user-input of any key on your console. Before pressing Enter on your console, you can insert your OTP in the page (and press submit if any) after which you can resume your execution from your console by pressing any key.
This shall create a manual-halt-like feature during which you can perform your manual interactions.

Using Capybara finder on HTML block

I had my Selenium tests navigating to a public dummy inbox to verify receipt of email deliveries and would perform some confirmation workflows there (password change, etc). It was becoming far too slow and unreliable.
I'm now using the API of a mail service to retrieve an email message. I'm able to search for the message I need and I'm able to retrieve the block of HTML that I'm usually inspecting.
Is there a way to use a Capybara finder on a block of HTML text?
You can turn a string of html into something Capybara can work with by doing
node = Capybara.string(the_html.to_s)
then you can call finders on node
node.find(...)

Not able to verify text after login with selenium Webdriver script

I am trying to automate login functionality with selenium webdriver using TestNG framework. I want to verify the text after login like Hi [username#gmail.com].
This is the order in which links are present on page :
TrackOrder | Notification | Singnup | Login
My method of doing:
I clicked on Login (login window appears) and I sign in. After login
the order in which links are present on page are :
TrackOrder | Notification | Hi[username#gmail.com]
For verifying text after login :
I am extracting the xPath of web element Hi[username#gmail.com] and using getText() method to verify actual and expected value.
Actual problem:
As soon as I run script , it logs in successfully but when I extract the text of web element it is printing signup. Though login is done successfully why is it showing up singup?
The reason for printing signup maybe that either the XPath you gave, is not explicitly identifying the username element and just give the element next to Notification element, or the signup element text is changing to username text after login.
So, better to create unique XPath for your element and wait for some time before using getText() method.
Thread.sleep() can be used if the text of the element is changing on login as selenium will execute the method before the change in text value.
Try to use Absolute Xpath of the Web Element.

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