we have a webpage with 2 date calender one is a current date the other is referencedate which autoupdates itself to display a day less than the current date.
issue-After selecting a date from a calender using webdriver click the other
calender which had to update the date automatically, does not update anymore.
The same actions if performed manually works.
Has anybody faced a similiar issue, this has become a blocker since with the autoupdate not working the entire application workflow fails.
removed sensitive data
package pages;
import java.util.Calendar;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.SendKeysAction;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.webdriven.SeleniumMutator;
import com.thoughtworks.selenium.webdriven.commands.SeleniumSelect;
import utils.Waitutils;
public class CalenderPage {
WebDriver driver;
static WebDriverWait wait;
public CalenderPage (WebDriver driver){
this.driver = driver;
}
public void cobDatepick(String date){
int count=0;
WebElement cob =driver.findElement(By.xpath("//*[#id='mainview']/div/div[2]/div[1]/div/options-panel-directive/div[2]/div[1]/div/div[1]/date-picker/div[2]/div/span"));
cob.click();
WebElement mLink = driver.findElement(By.xpath("//button[contains(#id,'datepicker')]"));
String date_dd_MM_yyyy[] = (date.split(" ")[0]).split("/");
int yearDiff = Calendar.getInstance().get(Calendar.YEAR) - Integer.parseInt(date_dd_MM_yyyy[2]);
mLink.click();
WebElement pLink = driver.findElement(By.xpath("//*[#id='mainview']/div/div[2]/div[1]/div/options-panel-directive/div[2]/div[1]/div/div[1]/date-picker/div[2]/div/ul/li[1]/div/table/thead/tr[1]/th[1]/button/i"));
if(yearDiff>0){
for(int i=0;i<yearDiff;i++){
pLink.click();
}
}
List<WebElement> months1 = driver.findElements(By.xpath("//*[#id='mainview']/div/div[2]/div[1]/div/options-panel-directive/div[2]/div[1]/div/div[1]/date-picker/div[2]/div/ul/li[1]/div/table/tbody/tr/td"));
months1.get(Integer.parseInt(date_dd_MM_yyyy[1])-1).click();
List<WebElement> dates = driver.findElements(By.xpath("//*[#id='mainview']/div/div[2]/div[1]/div/options-panel-directive/div[2]/div[1]/div/div[1]/date-picker/div[2]/div/ul/li[1]/div/table/tbody/tr//td"));
int total_nodes = dates.size();
for(int i=0; i<total_nodes; i++)
{
String dat = dates.get(i).getText();
if(dat.equals(date_dd_MM_yyyy[0])&& count>0)
{
dates.get(i).click();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.document.getElementById('date-picker-to').setAttribute('value', '30/06/2016');");
}
if(dat.equals(date_dd_MM_yyyy[0]))
{
count++;
}
}
}
}
The above class is called in the testscript
#BeforeTest
public void setup() throws Exception{
driver = new AppDriver("ie").getDriver();
driver.get("https://xyz");
driver.manage().window().maximize();
onCalenderPage = new CalenderPage (driver);
Waitutils.waitforCompletion(9000L);
}
#Test
public void test(){
onCalenderPage.cobDatepick("30/06/2016");
}
Related
I am running 2 classes using testNG.First class is running successfully but second class is failing with error:
"org.testng.TestNGException:Can't invoke public void TestScripts.NewAccountCreation.AccountCreation1(): either make it static or add a no-args constructor to your class"
If I add non argument constructor I am getting null pointer exception.
I am using Pagefactory to design my test cases.
Eclipse version:kepler
TestnG:6.9.9
Chrome version: 57.0.2987.133
Chrome driver: 2.27
Any suggestions would be greatly appreciated.
Please find the below code:
Page factory Code for Login Page:
/**
*
*/
package Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import Pages.LoginPage;
public class LoginPage {
WebDriver driver;
public LoginPage(WebDriver ldriver)
{
this.driver = ldriver;
}
#FindBy(xpath = "//input[contains(#id,'username-inputEl')]")
public WebElement username;
#FindBy(xpath = "//input[contains(#id,'password-inputEl')]")
public WebElement password;
#FindBy(xpath = "//span[contains(#id,'submit-btnInnerEl')]")
public WebElement LoginButton;
// Methods to perform actions
public void pCLogin(String Username, String Password, WebDriver driver)
{
username.sendKeys(Username);
password.sendKeys(Password);
LoginButton.click();
}
}
Page Factory code for Account creation page:
package Pages;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Accounttab {
WebDriver driver;
public Accounttab(WebDriver ldriver)
{
this.driver = ldriver;
}
#FindBy(xpath = "//span[#id='TabBar:AccountTab-btnWrap']")
public WebElement accountDropDown;
#FindBy(xpath = "//span[contains(#id,'AccountTab_NewAccount-textEl')]")
public WebElement NewAccount_Button;
#FindBy(xpath = "//textarea[contains(#id,'GlobalContactNameInputSet:Name-inputEl')]")
public WebElement Companyname;
#FindBy(xpath = "//input[contains(#id,'OfficialIDDV_Input-inputEl')]")
public WebElement FEINNumber;
#FindBy(xpath = "//input[contains(#id,'GlobalAddressInputSet:City-inputEl')]")
public WebElement City;
#FindBy(xpath = "//input[contains(#id,'GlobalAddressInputSet:PostalCode-inputEl')]")
public WebElement Zipcode;
#FindBy(xpath = "//a[contains(#id,'SearchLinksInputSet:Search')]")
public WebElement AccountsearchButton;
#FindBy(xpath = "//span[#class='x-btn-inner x-btn-inner-center'][contains(#id,'NewAccountButton-btnInnerEl')]")
public WebElement CreateNewButton;
#FindBy(css = "input[id='TabBar:AccountTab:AccountTab_AccountNumberSearchItem-inputEl']")
public WebElement AccountSearch;
#FindBy(xpath = "//tbody[contains(#id,'gridview')]//tr[1]//td[2]")
public WebElement SelectAccountNumber;
public void accountMouseHover(WebDriver driver) {
WebDriverWait wait1 = new WebDriverWait(driver, 10);
wait1.until(ExpectedConditions.visibilityOf(accountDropDown));
Actions builder2 = new Actions(driver);
builder2.moveToElement(accountDropDown).moveByOffset(50, 0).click()
.build().perform();
System.out.println("Dropdown is opened");
}
public void accountSearch(String AccountName, WebDriver driver)
{
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions .visibilityOf(NewAccount_Button));
driver.manage().window().maximize();
Actions builder1 = new Actions(driver);
builder1.moveToElement(NewAccount_Button).click().build().perform();
System.out.println("Clicked on New Accounr Button");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Companyname.sendKeys(AccountName);
try {
AccountsearchButton.click();
} catch (Exception e) {
System.out
.println("Please enter one of the minimum required fields: Company Name, FEIN"
+ e);
throw (e);
}
wait.until(ExpectedConditions .visibilityOf(CreateNewButton));
CreateNewButton.click();
}
}
Test Scripts1:
LoginClass:
**
package TestScripts;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import Pages.LoginPage;
import Utility.Configreader;
import Utility.GenericMethods;
public class Login {
WebDriver driver;
LoginPage Login_page;
#BeforeSuite
public void setUp() {
Configreader cr = new Configreader(
"H://Selenium//Selenium_ODSRegression//TestData//config.properties");
driver = GenericMethods.startBrowser("Chrome", cr.getURL());
Login_page = PageFactory.initElements(driver, LoginPage.class);
}
#Test
public void PClogin() {
Login_page.pCLogin("su", "gw", driver);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
**
Test Script2:
AccountCreation class:
package TestScripts;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import Pages.AccountFileSummary;
import Pages.Accounttab;
import Pages.CreateNewAccount;
public class NewAccountCreation {
WebDriver driver;
Accounttab Account_tab1;
CreateNewAccount NewAccount1;
AccountFileSummary AFS1;
public NewAccountCreation(WebDriver ldriver)
{
this.driver = ldriver;
Account_tab1 = PageFactory.initElements(driver, Accounttab.class);
}
#Test
public void AccountCreation1() {
Account_tab1.accountMouseHover(driver);
Account_tab1.accountSearch("WebDriver_Test1", driver);
}
}
driver class:
package Utility;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import Utility.Configreader;
public class GenericMethods {
public static WebDriver driver = null;
static Configreader cr = new Configreader(
"H://Selenium//Selenium_ODSRegression//TestData//config.properties");
public static WebDriver startBrowser(String browsername, String URL)
{
if (browsername.equalsIgnoreCase("Chrome"))
{
System.setProperty("webdriver.chrome.driver",
cr.getChromeDriverPath());
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--js-flags=--expose-gc");
options.addArguments("--enable-precise-memory-info");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-default-apps");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
else if (browsername.equalsIgnoreCase("IE"))
{
System.setProperty("webdriver.ie.driver", cr.getIEDriverPath());
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
}
driver.get(URL);
return driver;
}
}
Property file:
PCURL = http://biltipolicycenter.thehartford.com/pc/PolicyCenter.do
ChromeBrowserPath = C://Selenium//ChromeDriver 2.27//chromedriver.exe
IEBrowserPath = C://Selenium//IEDriverServer x86 2.53//IEDriverServer.exe
TestNG.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="TestScripts.Login"/>
<class name="TestScripts.NewAccountCreation"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
NewAccountCreation is a test class because it contains test method(s) and because you declare it as a class test in the testng.xml.
So, it means TestNG must be able to create an instance of the class and that's why TestNG is complaining.
You have 2 options:
Using a factory to explain to TestNG how to create test classes
(best option IMO)
Using a static shared driver instance (Login#driver is not
yet static)
BTW, your organization of classes looks a bit dirty and you should rework them a bit.
I am able to navigate to Flight Reservation page on website - http://www.spicejet.com/. Code is given below:
Code:
package Flight_Reservation;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class NewFlight_OneWay {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\Selenium_Practice\\EXEs\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.spicejet.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#id='ctl00_mainContent_rbtnl_Trip_1']")).click();
//Select origin
driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).sendKeys("DEL");
driver.findElement(By.linkText("Delhi (DEL)")).click();
//Select destination
driver.findElement(By.id("ctl00_mainContent_ddl_destinationStation1_CTXT")).sendKeys("BOM");
driver.findElement(By.linkText("Mumbai (BOM)")).click();
WebElement DateWidget = driver.findElement(By.id("ui-datepicker-div"));
List<WebElement> columns = DateWidget.findElements(By.tagName("td"));
for (WebElement cell: columns)
{
if (cell.getText().equals("24"))
{
cell.findElement(By.linkText("24")).click();
break;
}
}
Select AdultDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Adult")));
AdultDropdown.selectByValue("2");
Select ChildrenDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Child")));
ChildrenDropdown.selectByValue("1");
Select InfantDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Infant")));
InfantDropdown.selectByValue("1");
Select CurrencyDropdown = new Select(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")));
CurrencyDropdown.selectByValue("INR");
driver.findElement(By.id("ctl00_mainContent_btn_FindFlights")).click();
}
}
Question:
On flight selection page, I want to select flight record radio button with flight name 'SG 161'. Please let me know how can I achieve that?
Use following code to click on required radio-button:
driver.findElement(By.xpath('//input[#type="radio"][contains(#value, "SG~ 161")]')).click();
package com.test.utitlity;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
public class selectDropdown extends globalVariables {
public static void select(String locator, int indexValue) {
Select select= new Select(common.getObject(locator));
//select.selectByValue("selectValue");
System.out.println(indexValue);
select.selectByIndex(indexValue);
}
}
It is clicking the dropdown but not selecting the option.. Dropdown is empty.. How to resolve this?? But When I run the debug mode, it is working as expected. Added wait condition but getting IllegalStateException..
Can you try the below one and let me know what is the Result.
WebDriverWait Test_Wait = new WebDriverWait(driver, 10);
WebElement Drop_down = Test_Wait.until(ExpectedConditions.elementToBeClickable(common.getObject(locator)));
List<WebElement> lst2 = Drop_down.findElements(By.xpath(".//option[contains(text(),'Your_Option_Text')]"));
for (WebElement option : lst2)
{
if (!option.isSelected())
{
option.click();
}
}
I'm i\using webdriver version2.41 and browser is Firefox 28. I'm trying to find a listcount of elements present in the drop down list of a auto suggest textbox.Ex: in Google.co.in page i'm writing Banga to get the suggestions for Bangalore. Once i get the suggestion list then i want to dispay all the Auto suggested text on the screen. I have written the code, but don't know why its not working. I'm anew bie to selenium webdriver. Please help me. Here is my code :
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Banga");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
List<WebElement> lstobj = driver.findElements(By.xpath("//div[#class='gsq_a']/table/tbody/tr/td/span/b"));
System.out.println(lstobj.size());
for (int i = 0; i<lstobj.size();i++)
{
String p= lstobj.get(i).getText();
System.out.println(p);
}
}
}
I hope this helps u..
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class google{
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Banga");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
WebElement parent=driver.findElement(By.className("gssb_e"));
List<WebElement> child = parent.findElements(By.tagName("div"));
int size=child.size();
System.out.println(size);
for (int i =1; i<=size;i++)
{
String p= driver.findElement(By.xpath("//*[#id='gsr']/table/tbody/tr[1]/td[2]/table/tbody/tr["+i+"]/td/div/table/tbody/tr/td[1]")).getText();
System.out.println(p);
driver.close();
}
}
}
Edited the xpath used by you and the way you retrieved text :
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Banga");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
List<WebElement> lstobj = driver.findElements(By.xpath("//table[#class='gssb_m']/tbody/tr"));
System.out.println(lstobj.size());
for (int i = 0; i<lstobj.size();i++)
{
String p= lstobj.get(i).findElement(By.xpath("//span")).getText();
System.out.println(p);
}
}
driver.get("https://www.google.co.in/");
driver.findElement(By.xpath("//input[#class='gLFyf gsfi']")).sendKeys("Banga");
//This will also work USE descendant to get all child element
List<WebElement> printlist = driver.findElements(By.xpath("//ul[#role='listbox']//li/descendant::div[#class='sbl1']"));
System.out.println(printlist.size());
for ( WebElement list: printlist) {
//if you want to specify condition here you can
System.out.println(list.getText());
}
With below mentioned code,if the test case is pass-screenshot captured successfully and displayed in report.But when the test is failed--screenshot is not displayed.Even screenshot hyperlink is not displayed in report.Anybody can sort out the mistake in code?
package listeners;
import java.io.File;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import java.util.logging.Logger;
#Listeners
public class CountryChoserLayer extends TestListenerAdapter {
#Test(priority=1)
public void choseCountry() throws Exception{
driver.findElement(By.id("intselect")).sendKeys("India");
driver.findElement(By.xpath(".//*[#id='countryChooser']/a/img")).click();
//window.onbeforeunload = null;
Date date=new Date();
Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");
File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png");
FileUtils.copyFile(scrnsht, new File(NewFileNamePath));
System.out.println(NewFileNamePath);
Reporter.log("Passed Screenshot");
System.out.println("---------------------------------------");
System.out.println("Country choser layer test case-Success");
System.out.println("---------------------------------------");
}
public String baseurl="http://www.sears.com/shc/s/CountryChooserView?storeId=10153&catalogId=12605";
public WebDriver driver;
public int Count = 0;
#Test(priority=0)
public void openBrowser() {
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.get(baseurl);
}
#Test(priority=2)
public void closeBrowser() {
driver.quit();
}
#Override
public void onTestFailure(ITestResult result){
Reporter.log("Fail");
System.out.println("BBB");
//Reporter.setCurrentTestResult(result);
Date date=new Date();
Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");
File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//File scrFile = ((TakesScreenshot) WebDriver.globalDriverInstance).getScreenshotAs(OutputType.FILE);
String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png");
//System.out.println("AAA" + NewFileNamePath);
try {
//System.out.println("CCC");
FileUtils.copyFile(scrnsht,new File(NewFileNamePath));
System.out.println(NewFileNamePath);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("DDD");
e.printStackTrace();
}
Reporter.log("Failed Screenshot");
Reporter.setCurrentTestResult(null);
System.out.println("---------------------------------------");
System.out.println("Country choser layer test case Failed");
System.out.println("---------------------------------------");
}
#Override
public void onTestSkipped(ITestResult result) {
// will be called after test will be skipped
Reporter.log("Skip");
}
#Override
public void onTestSuccess(ITestResult result) {
// will be called after test will pass
Reporter.log("Pass");
}
}
Your onTestFailure method is not being called because you didn't specify listener for your test class. You are missing a value in #Listeners annotation. It should be something like
#Listeners({CountryChoserLayer.class})
You can find more ways of specifying a listener in official TestNg's documentation.
Another problem you are likely to encounter would be NullPointerException while trying to take screenshot in onTestFailure method. The easiest workaround for that would be changing the declaration of driver field to static. I run the code with those fixes and I got the report with screenshot.
I must add that in my opinion putting both test and listener methods into one class is not a good practice.