I am currently automating the add to cart process on the website "http://www.oyehappy.com" using TestNG POM Structure. I have encountered a NullPointer Exception while handling autosuggestion. The code for my POM Class is as follows:
public class productPage_POM {
public WebDriver driver;
@FindBy(id="tagsf2")
private WebElement city_txtbox;
public productPage_POM(WebDriver driver)
{
PageFactory.initElements(driver, this);
}
public void sel_adr()
{
city_txtbox.sendKeys("gan"); // facing nullpointer exception here
String xp = "//a[starts-with(text(),'gan')]";
List<WebElement> allList = driver.findElements(By.xpath(xp));
int count = allList.size();
System.out.println(count);
for (int i = 0; i < count ; i++)
{
String name = allList.get(i).getText();
System.out.println(name);
if(name.contains("Gandhinagar"))
{
allList.get(i).click();
break;
}
}
Below is the HTML structure:
<div class="pro_detail">
<div id="changeImage" class="pro_detail_image">
<div class="pro_detail_title">
<div class="pro_detail_size">
<div class="pro_detail_form">
<input id="cityIdHidden" type="hidden" value="CITY-1219">
<input id="hasShippingOption" type="hidden" value="">
<input id="cityIdHidden1" type="hidden" value="">
<input id="liCount" type="hidden" value="1">
<div id="hiddenField">
<div class="pro_detail_form_inn">
<label>1. Where do you want to deliver this?</label>
<input id="tagsf2" class="inp_pro_detail2 election ui-autocomplete-input" type="text" value="Enter City" onchange="showAddtocart();" onblur="WatermarkOnblur('Enter City','tagsf2');shippingOption();" onfocus="WatermarkOnfocus('Enter City','tagsf2')" autocomplete="off">
<span class="ui-helper-hidden-accessible" role="status" aria-live="polite">CITY-1219</span>
<div class="clear"></div>
<div id="cityNotAvailable" class="error_message hide" style="display: none;">
</div>
<div class="textbox">
</div>
<div class="pro_detail_form">
<div id="divDeliveryText" class="pro_detail_form" style="display:none;">