0
package AddUser;



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

    public class CheckFirstname 
    {
    public static WebDriver d;
        public static void main(String []args)throws Exception{
           System.setProperty("webdriver.firefox.marionette","pathToGeckodriver");
           FirefoxDriver driver=new FirefoxDriver();


           driver.get("http://54.68.159.204/qmsadm");
           driver.manage().window().maximize();

           WebElement element=driver.findElement(By.name("username"));
           element.sendKeys("geosony");
           element=driver.findElement(By.name("password"));
           element.sendKeys("1");
           element.submit();
           element=driver.findElement(By.linkText("Admin"));
           element.click();
           element=driver.findElement(By.linkText("Users"));
           element.click();
           element=driver.findElement(By.xpath("//*[@id='search-container']/a/i"));
           element.click();




           element= driver.findElement(By.id("username"));
           element.sendKeys("#!#!#$@#!$@!$@#$%#%^#$^^&%&$%*");

           driver.findElement(By.xpath("//*[@id='adduser']/div/form/div[2]/div/div/label")).click();
           Thread.sleep(5000);

           driver.findElement(By.id("hide-pop-msg")).click();

           element=driver.findElement(By.id("password"));
           element.sendKeys("#@#%$^&$^");
           element=driver.findElement(By.id("name"));
           element.sendKeys("#!#@$%@");
           Select stat=new Select(driver.findElement(By.id("role")));
           stat.selectByVisibleText("None");
           element=driver.findElement(By.id("email"));
           element.sendKeys("8678678");
           try
           {
               element.submit();
           } 
           catch(Exception e)
           {
               System.out.println("Element does not exist!");
           }



        }   
    }
  • If i understand your problem => Submit is a method we used in selenium where locators type = 'submit'. We cant check the element exist or not using the submit method although we can check it out by using exact xpath(Locators) of the element exist or not in the application with if else instead of submit method. else explain more – Shailendra Rathore Sep 9 '16 at 5:57
  • thanks i got it . its a html alert. so kindly you are appreciated – user21268 Sep 9 '16 at 6:42
3

Sometimes the below code will not work, rather it will throw an Element not found Exception

  if(objSbm.isDisplayed())
{
    System.out.println("Submit button has displayed");
    objSbm.click();
}
else
{
    System.out.println("Submit button did not displayed");
} 

The best way to tackle this issue is to use try-catch block

try
{
  objSbm.isDisplayed()
  System.out.println("Submit button has displayed");
  objSbm.click();
}
catch (exception e)
{
  System.out.println("Submit button did not displayed");
} 
| improve this answer | |
1

Write the inline comments while writing the code and write simple code, reduce the no of lines.

public class CheckFirstname {
    public static void main(String[] args) throws Exception {        
        System.setProperty("webdriver.firefox.marionette","pathToGeckodriver");

        FirefoxDriver driver = new FirefoxDriver();driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver, 40);
        Actions Act = new Actions(driver);
        // ----------------------------------------
        // Open Application
        driver.get("http://54.68.159.204/qmsadm");
        // Wait for title
        wait.until(ExpectedConditions.titleIs("Page title"))
        // Verify home Page
        if(driver.getTitle().equals("Page title"))
        {
            System.out.println("Home Page displayed");
        }
        // wait for user name,password,submit,users,admin and search
        WebElement objUN = driver.findElement(By.name("username"));
        WebElement objPWD = driver.findElement(By.name("password"));
        WebElement objSbm = driver.findElement(By.name("Submit"));
        WebElement objAdm = driver.findElement(By.linkText("Admin"));
        WebElement objUser = driver.findElement(By.linkText("Users"));
        WebElement objSearch = driver.findElement(By.xpath("//*[@id='search-container']/a/i"));
        // Verify UN & PWD
        if(objUN.isDisplayed()&&objPWD.isDisplayed())
        {
            System.out.println("UN,PWD are displayed");
        }
    }
}

To check the submit button exist or not you can write like this

if(objSbm.isDisplayed())
{
    System.out.println("Submit button has displayed");
    objSbm.click();
}
else
{
    System.out.println("Submit button did not displayed");
} 
| improve this answer | |
0

You can use the following code to check element is present in the views. create a boolean function with a boolean element as set to false. Pass the web element to the boolean function if the element is displayed change status of the boolean element to true else false will be returned

@Test
public void testLogin(){ 

  if(isElementPresent(errorLogin))
         {            
             System.out.println("Please enter valid email id and password");
         }
         else
         {
             System.out.println("Login successfull..........Enjoy");

         }
}
public boolean isElementPresent(WebElement ele)
     {
        boolean ElementPresent=false;
        try {
            if(ele.isDisplayed())
            {
                ElementPresent=true;
            }
        }
        catch(Exception e)
        {
            System.out.println("Element is not proeset " +e);
        }
        return ElementPresent;


     }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy