Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
836 views
by The go-to Tester (218 points)
edited by

I am automatationg test using Selenium Webdriver in Java Language. I am using TestNG framework. I want to run test one after another in sequencial way. But i cannot do and getting error messages. My code is-:

@BeforeMethod
       public void setup() throws InterruptedException {
      System.setProperty("webdriver.gecko.driver", "C://Selenium driver/geckodriver.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
              capabilities.setCapability("marionette", true);
              driver=new FirefoxDriver();
              driver.get("http://newtours.demoaut.com");
              driver.manage().window().maximize();
              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       }
       @Test
       public void signIn() {
              driver.findElement(By.name("userName")).sendKeys("vickysingh");
              driver.findElement(By.name("password")).sendKeys("password");
              driver.findElement(By.name("login")).click();
       }
       @Test
       public void logOut(){
       driver.findElement(By.linkText("SIGN-OFF")).click();
       }
       @AfterMethod
       public void teardown() {
       driver.close();     

       } 

My first test is Passed but second test Fails. Second test should start after first test. How can I achive this. 

 

by The go-to Tester (181 points)
Kindly post the error stacktrace you are getting. That will help us identify the issue.
by The go-to Tester (218 points)
My stack trace is as follows-:
Unable to locate element: SIGN-OFF
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'HOME-PC', ip: '192.168.0.10', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, firefoxOptions={args=[], prefs={}}, appBuildId=20161208153507, moz:firefoxOptions={args=[], prefs={}}, version=, platform=XP, proxy={}, command_id=1, specificationLevel=0, acceptSslCerts=false, processId=5856, browserVersion=50.1.0, platformVersion=6.1, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=firefox, takesScreenshot=true, takesElementScreenshot=true, platformName=windows_nt}]
Session ID: 51bf9fc5-2f1b-430f-8317-c6db0f9ae4ef
*** Element info: {Using=link text, value=SIGN-OFF}
    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:422)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByLinkText(RemoteWebDriver.java:425)
    at org.openqa.selenium.By$ByLinkText.findElement(By.java:246)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
    at myPackage.TestNg2.logOut(TestNg2.java:36)
    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:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:823)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:778)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1225)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1150)
    at org.testng.TestNG.runSuites(TestNG.java:1075)
    at org.testng.TestNG.run(TestNG.java:1047)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer

After every execution you are closing the browser. So, in the first test, you opened the browser, you logged in and closed the browser by using @AfterMethond.

WebDriver opens every new session with cleared cache. So, when you tried to log out, you were already logged out. So, it throws element not found exception.

You can change @BeforeMethod and @AfterMethod to @BeforeClass and @AfterClass. This will not close the browser after every test method execution.

Or, you should copy your sign in code into your logOut() method to sign in first to do sign out test.

Hope that helps!


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...