How to valid a login page using Java and Selenium WebDriver?

前端 未结 2 1668
太阳男子
太阳男子 2020-12-22 06:45

In my database I have the username = user@javachap.com and password = javachap

If I run the code below, it passes the test although the username and password does no

相关标签:
2条回答
  • 2020-12-22 07:39
    public class LoginPageTest extends IntegrationTest {
    
    private HtmlUnitDriver driver;
    
    @Before
    public void setup() throws MalformedURLException, UnknownHostException{
        driver = new HtmlUnitDriver(true);
        driver.get(System.getProperty("login.url"));
    }
    
    @Test
    public void testAuthenticationFailureWhenProvidingBadCredentials(){
        driver.findElement(By.id("username")).sendKeys("fakeuser");
        driver.findElement(By.id("password")).sendKeys("fakepassword");     
        driver.findElement(By.id("login")).click();
    
        assertTrue(driver.getCurrentUrl().endsWith("failed"));
    }
    
    @Test
    public void testAuthenticationSuccessWhenProvidingCorrectCredentials(){
        driver.findElement(By.id("username")).sendKeys("validuser");
        driver.findElement(By.id("password")).sendKeys("validpassword");
        driver.findElement(By.id("login")).click();
    
        assertTrue(driver.getCurrentUrl().endsWith("/<name_of_webapp>/"));
    }
    
    }
    

    That's how I do it for example.

    EDIT: I just noticed comments. Anyway my code shows how you test the actual login page with Selenium.

    0 讨论(0)
  • 2020-12-22 07:41
    public class Ace {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.setProperty("webdriver.chrome.driver", "D://jars//chromedriver.exe"); 
            WebDriver driver = new ChromeDriver();
            driver.get("https://your login url");
    
            driver.findElement(By.name("username")).sendKeys("enter username");  
            //pay attention here By.name or By.id, see  the page source properly
            driver.findElement(By.name("password")).sendKeys("enter password");
    
            driver.findElement(By.xpath("//button[@value='login']")).click();
    
            driver.findElement(By.name("participant")).sendKeys("BLRFC1");
            driver.findElement(By.xpath("//button[@type='submit']")).click();
        }
    }
    
    0 讨论(0)
提交回复
热议问题