I am trying to write a method to verify page title using page objects but I am unable to do it. Could you please help me out how to write a method to verify page title after searching something in the google page.
Here is my 2 classes.
Class 1
public class GoogleSearchPage {
protected WebDriver driver;
#FindBy(name="q")
private WebElement SearchBox;
#FindBy(name="btnG")
private WebElement SearchButton;
public void SearchFor(String SearchTerm)
{
SearchBox.sendKeys(SearchTerm);
SearchButton.click();
}
}
Class2
public class TestSearchResults {
WebDriver driver;
GoogleSearchPage page;
#BeforeClass
public void Launch() throws Exception
{
driver = new FirefoxDriver();
driver.get("http://google.com");
}
#AfterClass
public void Close()
{
driver.quit();
}
#Test
public void ResultPage() {
System.out.println(page);
page=PageFactory.initElements(driver, GoogleSearchPage.class);
page.SearchFor("selenium");
}
}
Could you please help how to verify the title after displaying the Search results
Note: I am using TestNG.
Thanks in Advance,
Shiva Oleti.
Try this:
assertEquals("Your Page Title" , driver.getTitle());
driver.getTitle() - Returns Title of your current page.
You can get & verify title of page using python webdriver. See below codes
driver.title('title name')
assert 'title name' in driver.title('title name')
It will verify the title name. if it is there title name test case pass else through assert error.
Create a constructor in each page object. The constructor:
sets the instance driver value
waits for page load to complete (e.g. loading spinner not visible, jQuery.active == 0, document.readyState == 'complete')
verifies the page title (see other answers)
Issue PageFactory.initElement(driver, this);
Therefore you don't need a separate method to verify title. In each step you will have a new YourPageObjectPage() which checks the title automatically. Think DRY.
Related
So i have create successful test of adding a card to my profile and now i would like ot verify if card was added so i would like to check if "Your card was successfully added" text appeared, however for some kind of a reason the className not located.
This is an iframe however i do enter it to add the card details and submit them, so trying to understand why it cant locate the follow:
Adding card
public void addCard(Card card) {
switchToCardIframe()
.fillInCardNumber(card.getCardNumber())
.fillInCardHolder(card.getCardHolderName())
.fillInExpiryDate(card.getExpiryDate())
.fillInCvv(card.getCvv())
.clickOnToggle()
.clickOnAddButton();
}
This is how i locate the text
#FindBy(css = "payment-state-message")
private WebElement cardAddedIndicator;
Test
#Test
public void shouldSuccessfullyAddCard() {
myCards.addCard(cardData());
assertTrue(myCards.getCardAddedIndicator().isDisplayed());
}
I have also tried to check if another element displayed by escaping the iframe but no luck so any help appreciated
driver.switchTo().defaultContent()
I have also attempted Thread.sleep() for 10 seconds before taking getting the text from the css locater but not luck
Here is the base page where wait initialized
public class BasePage {
public WebDriver driver;
protected Wait wait;
public BasePage(WebDriver driver) {
initializePage(driver);
this.wait = new Wait(driver);
}
final protected void initializePage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 20), this);
}
}
Your css is wrong, so instead of
#FindBy(css = "payment-state-message")
private WebElement cardAddedIndicator;
try this
#FindBy(css = ".payment-state-message")
private WebElement cardAddedIndicator;
also do not check for visibility by .isDisplay
rather have a text method called on it. Put some delay/wait as well.
String actualMessage = myCards.getCardAddedIndicator().getText();
String expectedMessage = "Your card was successfully added"
then assert these two based on their actual and expected behavior .
Conclusion :
When there is an iframe form and you submit it, it does not mean that its still the same iframe. In my case when I submitted the form then iframe disappeared however in order for for the element to be located I had to perform driver().switch().parentFrame()
FYI: First always check your locators and make sure they are right.
This is my HomePage of Page Object where I created WebElement of UserName method and now want to call it in test case case but it not working.
Showing me the error on element line.
public class HomePage {
WebDriver driver;
By username = By.xpath("/html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[1]/td[2]/input[1]");
By password = By.xpath("/html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[2]/td[2]/input[1]");
By login = By.xpath("/html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[3]/td[2]/input[1]");
static WebElement element = null;
//Experiment with webelement
public static WebElement UserName(WebDriver driver) {
element.findElement(By.xpath("/html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[1]/td[2]/input[1]"));
return element;
}
public static WebElement Password(WebDriver driver) {
element.findElement(By.xpath("/html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[2]/td[2]/input[1]"));
return element;
}
public static WebElement ClickLogin(WebDriver driver) {
element.findElement(By.xpath("/html[1]/body[1]/form[1]/table[1]/tbody[1]/tr[3]/td[2]/input[1]"));
return element;
}
}
These are the code of test class
//objLogin = new HomePage();
HomePage objLogin = new HomePage();
//input the username, password and click login.
objLogin.UserName(driver).sendKeys("mngr279645");
objLogin.Password(driver).sendKeys("YzarAzy");
objLogin.ClickLogin(driver).click();
Any help will be appreciated.....
First of all I don't understand why would you need WebDriver driver as you parameter in every selector when you are no using it and it shouldn't be there.
And second I suggest you study more of a PageObject architecture and using Xpath in general. In your comment above I still cannot see error message being shown.
By changing the scope in pom.xml from test to compile . It's Worked for me
I'm just starting to learn selenium java.
I had this test code, its just basically open google.com page and get its title, and assert the title. my problem is every time I run the test, the Firefox gets called twice. I already search around about the possible issue, tried some of the fix. but nothing works for me.. tried changing "#BeforeTest" to "#BeforeClass" and to "#Before" still the same.
firefox version: 55.0.3
selenium version: 3.5.3
geckodriver : 0.19.0
here's my code:
public class ATest {
public String baseURL = "http://google.com";
public WebDriver driver;
#BeforeTest
public void setBaseURL() {
driver = new FirefoxDriver();
driver.get(baseURL);
}
#Test
public void verifyHomePageTitle() {
setBaseURL();
String expectedTitle = "Google";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
}
#BeforeTest executes before tests so You need to remove setBaseURL() from Your test. It will be run before it anyway.
I haven't used Selenium with Java, only with Ruby. But my guess is that #BeforeTest directive makes setBaseURL() to be executed before each test. So here you have your first opened browser. Later when in your actual test what you do is you run setBaseURL() again, which opens second browser.
Remove setBaseURL() from verifyHomePageTitle(), or remove #BeforeTest
get method is called twice hence it loads page twice. Once in #BeforeTest and second in #Test by calling setBaseURL. Remove the setBaseURL from #BeforeTest, move the get method in actual #Test method and you should be fine
Here is what java doc says about get method
void get(java.lang.String url)
Load a new web page in the current browser window.
get
void get(java.lang.String url)
Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete. This will follow redirects issued either by the server or as a meta-redirect from within the returned HTML. Should a meta-redirect "rest" for any duration of time, it is best to wait until this timeout is over, since should the underlying page change whilst your test is executing the results of future calls against this interface will be against the freshly loaded page. Synonym for WebDriver.Navigation.to(String).
Parameters: url - The URL to load. It is best to use a fully qualified URL
public class ATest {
public String baseURL = "http://google.com";
public WebDriver driver;
#BeforeTest
public void setBaseURL() {
driver = new FirefoxDriver();
}
#Test
public void verifyHomePageTitle() {
driver.get(baseURL);
String expectedTitle = "Google";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
}
Links for reference
Java Doc : http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.html#get-java.lang.String-
I'm doing some automation bits for work and right now I'm trying to automate a purchase through our storefront that should go to the paypal sandbox and complete the purchase. Everything looks good and I know the general flow works but I'm having trouble finding the webElements when I get to the first PayPal page.
The PayPal side of the flow consists of 2 pages. One to input the login information and another one to confirm the purchase. The second page works perfectly but the first one always gives me "Unable to find element" when I tell it to look for the email/password field and the login button. If make the driver print out the current URL for debugging purposes it correctly prints the payPal URL so it is looking at the right site. I also tried putting a 30 seconds delay to make sure it wasn't a timing issue and I get the same problem.
Here's the class file in question:
public class PayPalLoginPage extends AbstractPaymentPage {
//AbstractPaymentPage extends from AbrstractPageObject
private WebElement email; //Element ID is email
private WebElement password; //Element ID is password
#FindBy(id = "btnLogin")
private WebElement loginButton;
public PayPalLoginPage(WebDriver driver) {
super(driver);
PageFactory.initElements(driver, this);
}
public PayPalPurchaseConfirmationPage login (PayPalInfo payPalInfo) {
email.sendKeys(payPalInfo.getEmail());
password.sendKeys(payPalInfo.getPassword());
loginButton.click();
this.waitForPayPalLoadingCurtainToDisappear();
return new PayPalPurchaseConfirmationPage(this.getDriver());
}
The way I'm calling this class is like this:
case PAYPAL_PURCHASE:
setDriver(new PayPalLoginPage(getDriver()).login(getPaymentMethod()).confirmPayPalPurchase());
break;
So, the flow works perfectly up until it gets to the first payPal page and it just stops saying it can't find any of those 3 elements. If I set it to just wait there and manually fill up the information then it picks right up on the next page and works from them on (finding all the elements on the 2nd payPal page and acting on them).
I get the same behavior if I put the findElement.By line inside the login method and also the same result regardless of whether I'm trying to find them using id, name, xpath or css.
Any idea on what I could be missing?.
You are just defining the email and followings as WebElement but not assigning them to any webelement. See the following:
private WebElement email = driver.findElement(By.id("email_text_box_id"));
private WebElement password = driver.findElement(By.id("password_text_box_id"));
private WebElement loginbutton = driver.findElement(By.id("login_button_id"));
if you want PageObject to assign WebElements then you need to call the initElement inside of a case:
public class PayPalLoginPage extends AbstractPaymentPage {
//AbstractPaymentPage extends from AbrstractPageObject
private WebElement email; //Element ID is email
private WebElement password; //Element ID is password
#FindBy(id = "btnLogin")
private WebElement loginButton;
public PayPalPurchaseConfirmationPage login (PayPalInfo payPalInfo) {
email.sendKeys(payPalInfo.getEmail());
password.sendKeys(payPalInfo.getPassword());
loginButton.click();
this.waitForPayPalLoadingCurtainToDisappear();
return new PayPalPurchaseConfirmationPage(this.getDriver());
}
}
Call the PayPal login function:
public class UsingPayPalLoginPage {
public static void main(String[] args) {
// Create a new instance of a driver
WebDriver driver = new HtmlUnitDriver();
// Create a new instance of the PayPall page
// and initialise any WebElement fields in it.
PayPalLoginPage page = PageFactory.initElements(driver, PayPalLoginPage.class);
// call login of paypal.
page.login(payPalInfo);
}
}
I'm calling IE driver to launch this webpage (Business Objects). I'm able to login with the credentials. I need to click on a element on the next page. Need help writing the java to read this element and click.
<span style="white-space:nowrap;" class="iconText" id="IconImg_Txt_btnListing">Document List</span>
This is what I got so far using firebug-firepath.
driver.switchTo().defaultContent();
pickObj = driver.findElement(By.cssSelector("#IconImg_Txt_btnListing"));
pickObj.click();
Update: Another attempt-
public class InitComp {
//private WebDriver driver;
#FindBy(how = How.CSS, using = "#IconImg_Txt_btnListing") private WebElement DocListBtn;
public void clickDocList() {
DocListBtn.click();
}
}
This class is called as-
InitComp init = new InitComp();
PageFactory.initElements(driver, init);
init.clickDocList();
This does not help either though. Throws me an exception - "ElementNotFound". This page happens to be the first page after login. Where am I going wrong?
Thanks.
Arya