Prem Nazir Prem Nazir - 23 days ago 5
Java Question

Selenium Automatic element click returns null

I have written a automation test case in selenium to test login page and should click forgot password link.Browser opens and goes to the url given but forgot password link isn't automatically clicking ,Can someone tell whats the problem with my code.

BaseCode

public class Forgetpsw {

WebDriver driver;

@BeforeTest
public void Browseropen() throws Exception {
Browsers startup=new Browsers();
startup.Initiating();
}

@Test
public void Forget(String username) throws Exception{
driver.findElement(By.xpath("//*[text()='Forgot Password?']")).click();
Thread.sleep(3000L);
driver.findElement(By.xpath("//*[@placeholder='User ID'][@id='forgotPasswordForm:j_idt13:0:username']")).sendKeys(username);
}
}


Browsers.java

public class Browsers {
public WebDriver driver;

public void Initiating() throws IOException {
Properties prop= new Properties();
InputStream fis = new FileInputStream("D:\\Workspace\\TMS\\selenium\\ZLive\\src\\datadriven.properties");
prop.load(fis);


System.out.println("Initiating Browser");

if (prop.getProperty("browser").equals("firefox"))
{
driver=new FirefoxDriver();
}
else
{
System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.get(prop.getProperty("url"));
}
}


data.properties

browser=firefox
url=http://abcd.xxx.in/login
username=32131
pasword=32131


Console :

[TestNG] Running:
C:\Users\ds\AppData\Local\Temp\testng-eclipse-42991103\testng-customsuite.xml

Initiating Browser

===============================================
Default test
Tests run: 0, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================


HTML CODE

<div class="loginRelative" id="secondDiv"><label class="loginText">Transport Management System</label>
<div class="logo-div">
<div class="loginimageBgStyle"><img src="images/indexImg.png" /></div>
</div>
<div class="absoluteLeft"></div>
<div class="absoluteRight"></div><span id="loginForm:loginPanel"><div id="loginForm:messages" class="ui-messages ui-widget errorValidationStyle" aria-live="polite"></div>

<div class="controls loginTextbox" id="thirdDiv"><div id="loginForm:j_idt16:0:j_idt18" class="ui-outputpanel ui-widget"><input id="loginForm:j_idt16:0:username" name="loginForm:j_idt16:0:username" type="text" placeholder="User ID" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" /><script id="loginForm:j_idt16:0:username_s" type="text/javascript">PrimeFaces.cw("InputText","widget_loginForm_j_idt16_0_username",{id:"loginForm:j_idt16:0:username",widgetVar:"widget_loginForm_j_idt16_0_username"});</script></div>
</div><div id="loginForm:j_idt16:0:j_idt20" aria-live="polite" class="ui-message ui-helper-hidden"></div><script id="loginForm:j_idt16:0:j_idt20_s" type="text/javascript">$(function(){PrimeFaces.cw("Message","widget_loginForm_j_idt16_0_j_idt20",{id:"loginForm:j_idt16:0:j_idt20",widgetVar:"widget_loginForm_j_idt16_0_j_idt20",target:"loginForm:j_idt16:0:username"});});</script>

<div class="controls loginPassword"><div id="loginForm:j_idt16:0:j_idt22" class="ui-outputpanel ui-widget"><input id="loginForm:j_idt16:0:password" name="loginForm:j_idt16:0:password" type="password" class="ui-inputfield ui-password ui-widget ui-state-default ui-corner-all" placeholder="******" /><script id="loginForm:j_idt16:0:password_s" type="text/javascript">$(function(){PrimeFaces.cw("Password","widget_loginForm_j_idt16_0_password",{id:"loginForm:j_idt16:0:password",widgetVar:"widget_loginForm_j_idt16_0_password"});});</script></div>
<!-- &lt;p:keyboard id="" value="" &gt;

&lt;/p:keyboard&gt; -->
</div><div id="loginForm:j_idt16:0:j_idt24" aria-live="polite" class="ui-message ui-helper-hidden"></div><script id="loginForm:j_idt16:0:j_idt24_s" type="text/javascript">$(function(){PrimeFaces.cw("Message","widget_loginForm_j_idt16_0_j_idt24",{id:"loginForm:j_idt16:0:j_idt24",widgetVar:"widget_loginForm_j_idt16_0_j_idt24",target:"loginForm:j_idt16:0:password"});});</script>

<div><a href="/forgotPassword" class="forgotPwdStyle">Forgot Password?</a>
</div>

<div class="loginBtnBgStyle"><button id="loginForm:j_idt28" name="loginForm:j_idt28" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only noBtnStyle" type="button"><span class="ui-button-icon-left ui-icon ui-c loginBtnImg"></span><span class="ui-button-text ui-c">ui-button</span></button><script id="loginForm:j_idt28_s" type="text/javascript">PrimeFaces.cw("CommandButton","widget_loginForm_j_idt28",{id:"loginForm:j_idt28",widgetVar:"widget_loginForm_j_idt28"});</script><button id="loginForm:loginButton" name="loginForm:loginButton" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only loginBtnStyle" onclick="return loginTms();;" type="button"><span class="ui-button-text ui-c">Login</span></button><script id="loginForm:loginButton_s" type="text/javascript">PrimeFaces.cw("CommandButton","widget_loginForm_loginButton",{id:"loginForm:loginButton",widgetVar:"widget_loginForm_loginButton"});</script>
</div></span><script type="text/javascript">loginTms = function() {PrimeFaces.ab({s:'loginForm:j_idt30',f:'loginForm',u:'loginForm',pa:arguments[0]});}</script>

<script>
function TMS_onLoginBtnClick(){

//alert("localhostinnnn");
var payroll = $("#payrollIframe").contents();
payroll.find("#username").val(document.getElementById("loginForm:j_idt15:0:username").value);
payroll.find("#password").val(document.getElementById("loginForm:j_idt15:0:password").value);
payroll.find("#user_accounts")[0].submit();
return false;
}


/**
This method will be called from payroll
iframe after success full login
*/
function loginSuccess(){
//alert("logged in");
//TODO: handle here
loginTms();
}

function loginFail(){

//TODO: handle here
}
</script>
<iframe id="payrollIframe" src="http://localhost/GOPayroll/?source=1" style="opacity:0; height:1px; width:1px;">
</iframe>

<div class="logoutMsgStyle">

</div>


</div>
</div>

Answer

Inside your Initiating method,you need to return the driver object and store it in your Forgetpsw class.

public void Initiating() throws IOException {
    Properties prop= new Properties();
    InputStream fis = new FileInputStream("D:\\Workspace\\TMS\\selenium\\ZLive\\src\\datadriven.properties");
    prop.load(fis);


    System.out.println("Initiating Browser");

    if (prop.getProperty("browser").equals("firefox"))
    {
        driver=new FirefoxDriver();
    }
    else
    {
        System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    driver.manage().window().maximize();
    driver.get(prop.getProperty("url"));
    return driver;   //return the driver object.
 }

And the Browseropen method will look like,

  public void Browseropen() throws Exception {
    Browsers startup=new  Browsers();
    driver = startup.Initiating();
  }