Jacobvu84 / selenium-vietnam-training-course

Questions Tracking
7 stars 5 forks source link

Kiểm tra việc sử dụng phím tab với selenium sử dụng java #32

Closed NTHang closed 6 years ago

NTHang commented 6 years ago

Em đang gặp phải lỗi khi kiểm tra việc sử dụng phím tab. Mọi người giúp em sao lại ko pass ạ. Code của em đây ạ

package GUI; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.Keys; //import org.openqa.selenium.Keys; //import org.openqa.selenium.WebDriverException; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test;

@SuppressWarnings("unused") public class KTTab { WebDriver driver; @BeforeClass public void setUp() { //driver = new ChromeDriver(); driver = new FirefoxDriver(); } @Test public void user_Tab() throws Exception { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

 driver.get("http://demoqa.com/registration/");
// driver.manage().window().maximize();
 driver.manage().window().setSize(new Dimension(1920,1080));

 WebElement firstField = driver.findElement(By.name("first_name"));
 WebElement secondField = driver.findElement(By.name("last_name"));
WebElement thirField = driver.findElement(By.name("radio_4[]"));
 WebElement forField = driver.findElement(By.name("checkbox_5[]"));
WebElement fireField = driver.findElement(By.name("dropdown_7"));
 WebElement sixField = driver.findElement(By.name("date_8[date][mm]"));
 WebElement evelementField = driver.findElement(By.name("date_8[date][dd]"));
 WebElement tField = driver.findElement(By.name("date_8[date][yy]"));
 WebElement phoneField = driver.findElement(By.name("phone_9"));
 WebElement userField = driver.findElement(By.name("username"));
 WebElement emailField = driver.findElement(By.name("e_mail"));
 WebElement fileField = driver.findElement(By.name("profile_pic_10"));

 WebElement descriplField = driver.findElement(By.name("description"));
 WebElement passField = driver.findElement(By.name("password"));
 WebElement comfirmpassField = driver.findElement(By.id("confirm_password_password_2"));

 WebElement submit = driver.findElement(By.name("pie_submit"));
 //WebElement //webElement;
//webElement.sendKeys(Keys.TAB);
 //Start with the first field
 firstField.sendKeys();
 //Verify that we in the first field
 if(driver.switchTo().activeElement().equals(firstField))
     System.out.println("First element is in a focus");
 else
     //Add Assertion here - stop execution
     System.out.println("ASSERTION - first element not in the focus");

 //Object Keys;
firstField.sendKeys(Keys.TAB);

 //Verify that we in the second field
 if(driver.switchTo().activeElement().equals(secondField))
     System.out.println("Second element is in a focus");
 else
     //Add Assertion here - stop execution
     System.out.println("ASSERTION - second element not in the focus");

 secondField.sendKeys(Keys.TAB);

 if(driver.switchTo().activeElement().equals(thirField))
     System.out.println("thir element is in a focus");
 else
     //Add Assertion here - stop execution
     System.out.println("ASSERTION - thr element not in the focus");
//-----------------------------------------
 thirField.sendKeys(Keys.TAB);

 if(driver.switchTo().activeElement().equals(forField))
     System.out.println("for element is in a focus");
 else
     //Add Assertion here - stop execution
     System.out.println("ASSERTION - for element not in the focus");
//-----------------------------------------

 forField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(fireField))
         System.out.println("fire element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - fire element not in the focus");
    //-----------------------------------------

     fireField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(sixField))
         System.out.println("six element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - six element not in the focus");
    //-----------------------------------------

     sixField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(evelementField))
         System.out.println("element element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - elenment element not in the focus");
    //-----------------------------------------
     evelementField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(tField))
         System.out.println("tfile element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - submit element not in the focus");
    //-----------------------------------------
     tField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(phoneField))
         System.out.println("phone element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - submit element not in the focus");
    //-----------------------------------------
     phoneField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(userField))
         System.out.println("userfile element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - submit element not in the focus");

     userField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(emailField))
         System.out.println("emailfile element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - submit element not in the focus");
     emailField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(fileField))
         System.out.println("filefiled element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - submit element not in the focus");
     fileField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(descriplField))
         System.out.println("Descrip  element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - submit element not in the focus");

     //-----------------------
     descriplField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(passField))
         System.out.println("passfile element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - submit element not in the focus");
     //-----------------------
     passField .sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(comfirmpassField))
         System.out.println("comfirmpass element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - submit element not in the focus");
     //-----------------------
     comfirmpassField.sendKeys(Keys.TAB);

     if(driver.switchTo().activeElement().equals(submit))
         System.out.println("submit element is in a focus");
     else
         //Add Assertion here - stop execution
         System.out.println("ASSERTION - submit element not in the focus");
     //-----------------------

 //Click the button 
     submit.click();
 }

 //Need be closed also in case the assertion - use
 @AfterClass public void afterTest()
    {
        driver.close();

    }

}

lỗi [RemoteTestNG] detected TestNG version 6.12.0 1508294503860 geckodriver INFO geckodriver 0.18.0 1508294503871 geckodriver INFO Listening on 127.0.0.1:16718 log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 1508294504695 geckodriver::marionette INFO Starting browser C:\Program Files\Mozilla Firefox\firefox.exe with args ["-marionette"] [GPU 5824] WARNING: pipe error: 109: file c:/builds/moz2_slave/m-rel-w32-00000000000000000000/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346 1508294509690 Marionette INFO Listening on port 2321 Oct 18, 2017 9:41:50 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C JavaScript error: http://demoqa.com/registration/, line 514: ReferenceError: accordion is not defined First element is in a focus Second element is in a focus thir element is in a focus ASSERTION - for element not in the focus ASSERTION - fire element not in the focus six element is in a focus element element is in a focus tfile element is in a focus phone element is in a focus userfile element is in a focus emailfile element is in a focus filefiled element is in a focus FAILED: user_Tab org.openqa.selenium.InvalidArgumentException: File not found: ? Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:54:15.039Z' System info: host: 'H8NDVZIVSWJAXSI', ip: '192.168.1.105', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_111' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{moz:profile=C:\Users\ADMINI~1\AppData\Local\Temp\rust_mozprofile.LEasJ72O9AgI, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, platform=XP, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=53.0.3, platformVersion=6.1, moz:processID=4056, browserName=firefox, javascriptEnabled=true, platformName=XP}] Session ID: 0544864f-21b2-4ed9-89f3-02765e4d37b5 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:646) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:275) at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:99) at GUI.KTTab.user_Tab(KTTab.java:193) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108) at org.testng.internal.Invoker.invokeMethod(Invoker.java:669) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:776) at org.testng.TestRunner.run(TestRunner.java:634) at org.testng.SuiteRunner.runTest(SuiteRunner.java:425) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385) at org.testng.SuiteRunner.run(SuiteRunner.java:334) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318) at org.testng.TestNG.runSuitesLocally(TestNG.java:1243) at org.testng.TestNG.runSuites(TestNG.java:1161) at org.testng.TestNG.run(TestNG.java:1129) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

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

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

Jacobvu84 commented 6 years ago

Code mà nhìn như này thì anh cũng thua thôi.

Nhưng mà Verify trong testing không ai dùng lệnh if cả. Người ta dùng ngay phương thức assertXXX trong TestNG hoặc Junit.

thanh1985tq commented 6 years ago

sao đoạn code này ko bị lỗi compile nhỉ, annotation Test và BeforeTest viết thường mà IDE vẫn cho pass mới kỳ =))

Jacobvu84 commented 6 years ago

@thanh1985tq cho do lỗi lúc copy lên github thôi. Gihub tự formatting lại, chữ thường và chữ hoa đôi khi tự nó đổi.