Assert name field of registration process with sendKeys java selenium - selenium

I have an automation running on a website with registration process.
I need to assert all fields on registration: name, email, pass, confirmPass.
If for example I run my test as follows it always fails since the Actual always remains empty. what am I missing here with the sendKeys ?
InsertXpathAndClick
InsertIDAndKeysToSend
etc are shortcuts to find elements and click or send keys (they operate as expected on other parts of my program)
#Test
public void TestAssertName() {
SingeltonDriver.driver.navigate().to("https://buyme.co.il");
SingeltonDriver.driver.manage().window().maximize();
SingeltonDriver.driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
InsertXpathAndClick("//*[#id=\"ember676\"]/div/ul[1]/li[3]/a/span[2]");
InsertXpathAndClick("//*[#id=\"ember650\"]/div/div[1]/div/div/div[3]/p/span");
SingeltonDriver.driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
//This is the interesting part of checking the name val that i sent VS the one that i get by getText.
InsertIDAndKeysToSend("ember1179", "Kate");
WebElement ActualName = SingeltonDriver.driver.findElement(By.id("ember1179"));
String ActualNameUpdated = ActualName.getText();
Assert.assertEquals("Kate", ActualNameUpdated);
}

Related

Open verification code sent in mail box then copy this code and paste it into verification code field in selenium testing

I am new to selenium web testing i have automated a Sign-In process for a web base application. now making it for Sign Up process i am stuck at a point where a verification code is sent to a mail address and then i have to copy that into my verification code field and proceed further
As i have searched so far i came to know about the mailosaur server but unable to copy that email verification code into my automated web browser. i also searched for the tutorials but unable to find any useful resource. also i want to generate random emails that part is also not getting in my mind.
As i am new to selenium so it is requested to please provide detail answer so i can understand it better, Thanks in advance, working on Intellij, Mavaen (Java)
You can use mailinator.com. No need to register or create a mail box. In your app just enter email with made up name #mailinator.com (asad1#mailinator.com, asadXY#mailinator.com, whatever).
To collect confirmation link (double opt in) I'm using this:
public class Mailinator {
public WebDriver driver;
public Mailinator(WebDriver driver) {this.driver = driver;}
public String urlMailinator = "https://www.mailinator.com/";
public WebDriverWait waitSec(WebDriver driver, int sec) {return new WebDriverWait(driver, sec);}
public static String doubleOptInLink = null;
public String getDoubleOptInLink() {return doubleOptInLink;}
public void setDoubleOptInLink (String doubleOptInLink) {Mailinator.doubleOptInLink = doubleOptInLink;}
public void collectDoubleOptInLink(String userEmail, int expectedNumberOfDeliveredEmails) throws InterruptedException {
driver.get(urlMailinator);
WebElement fldInbox = waitSec(driver, 5).until(ExpectedConditions.elementToBeClickable(By.id("inboxfield")));
fldInbox.sendKeys(userEmail);
WebElement btnGo = driver.findElement(By.xpath("/html/body/section[1]/div/div[3]/div[2]/div[2]/div[1]/span/button"));
btnGo.click();
waitSec(driver, 600).until(ExpectedConditions.numberOfElementsToBe((By.xpath("//*[#id=\"inboxpane\"]/div/div/div/table/tbody/tr")), expectedNumberOfDeliveredEmails));
WebElement lastMailLink = driver.findElement(By.xpath("//*[#id=\"inboxpane\"]/div/div/div/table/tbody/tr"));
lastMailLink.click();
Thread.sleep(3000);
driver.switchTo().frame(driver.findElement(By.id("msg_body")));
setDoubleOptInLink(driver.findElement(By.xpath("//*[#id=\"intro\"]/tbody/tr/td/a")).getAttribute("href"));
}
}
In my scenario:
register to webapp with new made up email, confirmation email is send
using collectDoubleOptInLink(email, 1); is the confirmation link set as doubleOptInLink
calling another method to go to the confirmation link with getDoubleOptInLink();
Sure you will need change what string comes to setDoubleOptInLink();
In specific cases don't forget to setDoubleOptInLink(null);.

GetAttribute of WebElement in Selenium Firefox Driver Returns Empty

So I have a test that was written, and the test runs and works using the Selenium ChromeDriver just fine. I was tasked with getting the tests working on the FirefoxDriver as well.
Part of the test we input text into a login field, and then check the login field to make sure it was input. The way we check the field is like this
public virtual string Text => WebElement.GetAttribute("value");
while (!Text.Equals(inputText) && count++ < 3)
This works perfectly fine in Chrome. However it does not in Firefox. When I debug the test, it shows that Text is "" or empty/blank. If I open Firefox, I can do this document.getElementById("login").value and it returns the correct value.
Is WebElement.GetAttribute implemented differently in the FirefoxDriver or am I just missing something?
It's hard to say in your case why is not working on Firefox, there is no different implementation between browsers. You can try alternate solution using IJavascriptExecutor instead as below :-
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string Text = (string)js.ExecuteScript("return arguments[0].value", WebElement);
The Selenium protocol to get an attribute/property has evolved with Selenium 3.
With Selenium 2, the method WebElement.GetAttribute(...) returns the HTMLElement property when present and the attribute otherwise.
With Selenium 3, there's a distinctive command to get the property and one for the attribute :
https://www.w3.org/TR/webdriver/#get-element-property
In your case it seems that you are using the geckodriver (Selenium 3) with a client limited to the Selenium 2 specs. It would explain why the property is not returned.
To make it work, you can either upgrade your C# client to V3.0.0-beta2:
https://github.com/SeleniumHQ/selenium/blob/master/dotnet/CHANGELOG
https://github.com/SeleniumHQ/selenium/commit/a573338f7f575ccb0069575700f2c059dc94c3b8
Or you can implement your own GetProperty in a method extension:
static string GetProperty(this IWebElement element, string property) {
var driver = (RemoteWebDriver)((RemoteWebElement)element).WrappedDriver;
var result = (IList)driver.ExecuteScript(#"
var element = arguments[0], property = arguments[1];
if (property in element) return [true, '' + element[property]];
return [false, 'Missing property: ' + property];
", element, property);
bool succeed = (bool)result[0];
if (!succeed) throw new WebDriverException((string)result[1]);
return (string)result[1];
}
Usage:
string value = driver.FindElement(...).GetProperty("value");

How to verify whether google search results consist of a specific text using selenium?

I want to verify if the google search results contain the specific text or not.
Following is my code:
public static void main(String[] args) {
System.out.println("setting the driver path");
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Arushi\\Desktop\\Arushi\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
WebElement textbox = driver.findElement(By.name("q"));
textbox.sendKeys("stack");
WebElement button = driver.findElement(By.name("btnG"));
button.click();
String bodyText = driver.findElement(By.tagName("body")).getText();
Assert.assertTrue("Text not found!", bodyText.contains("stackoverflow.com"));
But the above code does not give correct result. The getText() method gets the body text of the page "google.com" instead of the page loaded after performing the search.
I want to know 2 things:
1. Why does getText() above is picking up the body text from google.com
2. What is the correct way to search for the specific text in the google search results.
Note: I also tried driver.getPageSource().contains() method but even that's not giving the correct result.
I would attack this by identifying the exact elements that represent search results on the DOM, then loop over each instance of a search result to validate the text for my search term. Simply searching the full page for some text opens up some opportunities for inaccurate results.
By looking at the DOM of a google search result page, we can see that each of the search results live in a node with class .rc. Children of that node, with classes .r and .s represent the result name and description, respectively.
I'll assume for this example that you'd like to check the search result names for your search term, but you should be able to easily refactor the code below to your specific needs.
public static void main(String[] args) {
// First, let's declare our search term
private String searchTerm = "Selenium";
// Then, let's start our WebDriver and navigate to google
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
// Next, we'll execute the search
WebElement searchField = driver.findElement(By.name("q"));
searchField.sendKeys(searchTerm);
WebElement searchButton = driver.findElement(By.name("btnK"));
searchButton.click();
// Now, let's gather our search results
List<WebElement> results = driver.findElements(By.cssSelector(".r"));
// Finally, we'll loop over the list to verify each result link contains our term
for (int i = 0; i < results.size(); i++) {
Assert.assertTrue(results.get(i).getText().contains(searchTerm), "Search result validation failed at instance [ + i + ].");
}
}
You may need to add any appropriate waits. This can also be further tweaked to ensure each result will be evaluated before a final pass/fail with a full output of each individual result that did not match the term, but I'll leave that to you to implement. Hopefully this will be a good start for you.

how effectively i can use ui automation recorded test cases for other releases of the application

I've web application, I want recorded test cases and play back that cases.
1st release of the application, I've login module which has user name and password and recorded 500 test cases for entire application. Among 500 test cases 200 test cases are using logging by username and password.
2nd release of the application, I've login module which has only username, so I want use previous recorded test cases by modifications not like go to all the test cases change the password field. Here I'm having some requirements for the testing framework
Can I get what are test cases will effect by changing field like in above example?
Is there any way to update in simple, not going like in all the files and changing
I've used different UI Automation testing tools and record & Play back options are very nice, but I could not find the way I want in the UI Automation test framework.
Is there any Framework available which does the job for me?
Thanks in advance.
This is a prime example of why you never should record a selenium test case. Whenever you want to update something like login you have to change them all.
What you should do is create a test harness/framework for your application.
1.Start with creating a class for each webpage with 1 function for each element you want to be able to reach.
public By username(){
return By.cssSelector("input[id$='username']"); }
2.Create helper classes where you create sequences which you use often.
public void login(String username, String password){
items.username().sendkeys(username);
items.password().sendkeys(password);
}
3.In your common test setup add your login function
#BeforeMethod(alwaysRun = true)
public void setUp() {
helper.login("user","password");
}
This give you the opportunity to programmaticly create your test cases. So for example if you want to use the same test cases for a different login module where password element is not present it could be changed like this.
items.username().sendkeys(username);
if(isElementPresent(items.password())
items.password().sendkeys(password);
The function "isElementPresent" could look like this
public boolean isElementPresent(By locator){
try {
driver.findElement(locator);
logger.trace( "Element " + stripBy(locator) + " found");
} catch (NoSuchElementException e) {
logger.trace( "Element " + stripBy(locator) + " not found");
return false;
}
return true;
}

How to use "isTrue" method of "Text" class?

I am unable to use "isTrue" method of text class
Here is the "Text" class detail
http://selenium.googlecode.com/git/docs/api/java/index.html
// Code i have written
public void researchSelenium(){
driver.get(baseUrl);
ConditionRunner.Context cont = new Research();
Text obj = new Text("Why implement a customer referral program?");
System.out.println(obj.isTrue(cont));
driver.close();
driver.quit();
I dont know what to do here
ConditionRunner.Context cont = new Research(); //After "new" what should i write?
object of ConditionRunner.Context will pass to "isTrue" method
I'm going to make a few presumptions here:
driver is a selenium web driver instance
You are trying to find if a text field is present in your web page
You know what the text is, but not its xpath (or at least do not care "where" it is).
In this case, you just have a minor syntaxical error
public void researchSelenium()
{
driver.get(baseUrl);
//Not sure what this is doing.
ConditionRunner.Context cont = new Research();
//Small chgange here
string obj = "Why implement a customer referral program?";
System.out.println(driver.isTextPresent(obj));
driver.close();
driver.quit();
}
NB. the above is "free coded" and I've not tested/complied it. Feel free to edit if there's a minor problem.
APPEND:
Personally I'd use NUnit to handle tests, so in that case I'd use:
Assert.isTrue(driver.isTextPresent(obj));
To test if that text was actually present, but how you're running your tests is not something that's stated in your question.