How to check username field if else condition.

Hello I an fresher. I want to check username field if else condition in junit script. if username is blank then run the driver.quit and if username already submit then run the driver.quit. Another run the program.
so please tell me how to check.

Default Asked on November 21, 2014 in Selenium WebDriver.

@sumit,

Unable to understand your question. Please elaborate your question so people can answer to your question.

on November 22, 2014.

sir I want to check two condition first is blank value and secound is wrong value. Please check
package esong;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class free_recharge
{
WebDriver driver;
String url = “http://www.easymobilerecharge.com/logmein.php”;

@BeforeTest
public void start()
{
driver = new FirefoxDriver();
driver.get(url);

}

@org.testng.annotations.Test
public void recharge_login() throws Exception
{
driver.findElement(By.xpath(“//input[@name=’userid’]”)).sendKeys(“sumitkv888@gmail.com”);
Thread.sleep(3000);
String i = driver.findElement(By.xpath(“//input[@name=’userid’]”)).getText();
System.out.println(i); // It is not print, I think “i” variable is not
// holding the sendKeys(“sumitkv888@gmail.com”)
// value.
String z = “asdfg”;

System.out.println(i);
if (i.isEmpty())
{
driver.quit();

}
else if (i.equals(“sumitkv8881@gmail.com”))
{

driver.findElement(By.xpath(“//input[@name=’password’]”)).sendKeys(“12345678”);
driver.findElement(By.xpath(“//input[@id=’login_button’]”)).click();
}
else
{
System.out.println(“username is not valid”);
driver.quit();
}
}

@AfterTest
public void close()
{
driver.quit();
}

}

on November 25, 2014.
Add Comment
  • 1 Answer(s)

    Hi Sumit,

    I am not 100% sure what is your question, but as per my understanding posting my code. Please let me know this will for you or not.

    
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    import org.testng.annotations.Test;
    public class GetAttribute {
    @Test
    
    public void GetAttributeValue() throws InterruptedException
    
    {
    
    WebDriver d = new FirefoxDriver();
    
    d.manage().window().maximize();
    
    d.get("http://www.facebook.com");
    
    d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    
    String username= d.findElement(By.xpath(".//*[@id='email']")).getAttribute("value");
    
    System.out.println("Username is: "+ username);
    
    if(username.isEmpty()){
    
    d.findElement(By.xpath(".//*[@id='email']")).sendKeys("USERNAME");
    
    System.out.println("USER NAME is: " + d.findElement(By.xpath(".//*[@id='email']")).getAttribute("value"));
    
    }else{
    
    System.out.println("User Name Not Entered");
    
    d.quit();
    
    }
    
    }
    
    }
    
    
    Vice Professor Answered on November 22, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.