51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 5069|回复: 4
打印 上一主题 下一主题

[求助] selenium+java用testng,报错了,不知是哪里错了

[复制链接]
  • TA的每日心情
    郁闷
    2020-8-19 15:27
  • 签到天数: 5 天

    连续签到: 3 天

    [LV.2]测试排长

    跳转到指定楼层
    1#
    发表于 2017-3-20 11:31:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    贴上2份代码,第一份运行过了,第二份运行失败,正在学代码,不知是哪里错了,求解。

    (1)代码一:
    import java.util.regex.Pattern;
    import java.util.concurrent.TimeUnit;
    import org.testng.annotations.*;
    import static org.testng.Assert.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;
    import static org.testng.Assert.fail;

    public class baidu2 {
        private WebDriver driver;
          private String baseUrl;
          private boolean acceptNextAlert = true;
          private StringBuffer verificationErrors = new StringBuffer();

          @Test
          public void testBaidu() throws Exception {
              System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");  
              WebDriver driver = new FirefoxDriver();  
              driver.get("https://www.baidu.com");
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.findElement(By.id("kw")).click();
            driver.findElement(By.id("kw")).clear();
            driver.findElement(By.id("kw")).sendKeys("一天");
            driver.findElement(By.id("su")).click();
          
              driver.quit();
          }
         
    }
    (1)运行结果:
    1489979034518    geckodriver    INFO    Listening on 127.0.0.1:34701
    三月 20, 2017 11:03:54 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
    信息: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
    1489979035097    mozprofile::profile    INFO    Using profile path C:\Users\admin\AppData\Local\Temp\rust_mozprofile.xUENrzi2iZn1
    1489979035104    geckodriver::marionette    INFO    Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe
    1489979035124    geckodriver::marionette    INFO    Connecting to Marionette on localhost:56903
    1489979036125    Marionette    INFO    Listening on port 56903
    三月 20, 2017 11:03:58 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
    信息: Detected dialect: W3C
    1489979039470    Marionette    WARN    Using deprecated data structure for setting timeouts
    1489979039669    Marionette    INFO    New connections will no longer be accepted
    JavaScript error: https://sp0.baidu.com/5a1Fazu8AA ... amp;_=1489979038420, line 1: TypeError: jQuery1102008973990459105563_1489979038417 is not a function
    [Child 14428] WARNING: pipe error: 232: file c:/builds/moz2_slave/m-rel-w32-00000000000000000000/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 512
    [Child 14428] ###!!! ABORT: Aborting on channel error.: file c:/builds/moz2_slave/m-rel-w32-00000000000000000000/build/src/ipc/glue/MessageChannel.cpp, line 2143
    [NPAPI 12364] ###!!! ABORT: Aborting on channel error.: file c:/builds/moz2_slave/m-rel-w32-00000000000000000000/build/src/ipc/glue/MessageChannel.cpp, line 2143
    三月 20, 2017 11:04:01 上午 org.openqa.selenium.os.UnixProcess destroy
    严重: Unable to kill process with PID 568
    PASSED: testBaidu

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


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

    (2)代码二:
    import java.util.regex.Pattern;
    import java.util.concurrent.TimeUnit;
    import org.testng.annotations.*;
    import static org.testng.Assert.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;
    import static org.testng.Assert.fail;


    public class baidu {
      public  WebDriver driver;
      private boolean acceptNextAlert = true;
      private StringBuffer verificationErrors = new StringBuffer();

      @BeforeClass(alwaysRun = true)
      public  void setUp() throws Exception {
          System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");  
          WebDriver driver = new FirefoxDriver();  
          driver.get("https://www.baidu.com");
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }

      @Test
      public void testBaidu() throws Exception {

        driver.findElement(By.id("kw")).click();
        driver.findElement(By.id("kw")).clear();
        driver.findElement(By.id("kw")).sendKeys("一天");
        driver.findElement(By.id("su")).click();
      }

      @AfterClass(alwaysRun = true)
      public void tearDown() throws Exception {
          driver.quit();
      }
    }

    (2)运行结果:
    1489979421613    geckodriver    INFO    Listening on 127.0.0.1:34500
    三月 20, 2017 11:10:22 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
    信息: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
    1489979422178    mozprofile::profile    INFO    Using profile path C:\Users\admin\AppData\Local\Temp\rust_mozprofile.6tPTLNnXQZ93
    1489979422185    geckodriver::marionette    INFO    Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe
    1489979422219    geckodriver::marionette    INFO    Connecting to Marionette on localhost:57190
    1489979423146    Marionette    INFO    Listening on port 57190
    三月 20, 2017 11:10:25 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
    信息: Detected dialect: W3C
    1489979426684    Marionette    WARN    Using deprecated data structure for setting timeouts
    FAILED CONFIGURATION: @AfterClass tearDown
    java.lang.NullPointerException
        at com.test.webkyy.baidu.tearDown(baidu.java:39)
        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.invokeConfigurationMethod(Invoker.java:523)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:146)
        at org.testng.internal.TestMethodWorker.invokeAfterClassMethods(TestMethodWorker.java:212)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
        at org.testng.TestRunner.privateRun(TestRunner.java:744)
        at org.testng.TestRunner.run(TestRunner.java:602)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
        at org.testng.SuiteRunner.run(SuiteRunner.java:289)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
        at org.testng.TestNG.runSuites(TestNG.java:1144)
        at org.testng.TestNG.run(TestNG.java:1115)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

    FAILED: testBaidu
    java.lang.NullPointerException
        at com.test.webkyy.baidu.testBaidu(baidu.java:31)
        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:661)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:744)
        at org.testng.TestRunner.run(TestRunner.java:602)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
        at org.testng.SuiteRunner.run(SuiteRunner.java:289)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
        at org.testng.TestNG.runSuites(TestNG.java:1144)
        at org.testng.TestNG.run(TestNG.java:1115)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)


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


    ===============================================
    Default suite
    Total tests run: 1, Failures: 1, Skips: 0
    Configuration Failures: 1, Skips: 0
    ===============================================
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2024-5-16 12:02
  • 签到天数: 1181 天

    连续签到: 1 天

    [LV.10]测试总司令

    推荐
    发表于 2017-3-20 12:46:56 | 只看该作者
    第二个代码中的tearDown方法中使用的driver变量没有初始化。在setUP()中初始化的driver变量是个局部变量只能在setUp方法中使用,在tearDown方法中不能使用。
    你的代码存在两处问题:
    1.已经在类中声明了driver变量,但是没有初始化(没有使用)。
    2.在tearDown中又一次声明了driver变量,此为局部变量,不能作为全局使用。

    评分

    参与人数 1测试积点 +10 收起 理由
    lsekfe + 10 积极回复获得测试积点10 赶快去商城换取奖.

    查看全部评分

    回复 支持 1 反对 0

    使用道具 举报

  • TA的每日心情
    郁闷
    2020-8-19 15:27
  • 签到天数: 5 天

    连续签到: 3 天

    [LV.2]测试排长

    3#
     楼主| 发表于 2017-3-20 13:19:42 | 只看该作者
    清晨一缕阳光 发表于 2017-3-20 12:46
    第二个代码中的tearDown方法中使用的driver变量没有初始化。在setUP()中初始化的driver变量是个局部变量 ...

    谢谢
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    5#
    发表于 2024-1-2 04:12:35 | 只看该作者
    Obviously, It can be good to play with friends also - and less difficult to encourage them to join you with no upfront Price! Never feel undesirable about hunting up guides, considering the fact that many of the customization possibilities could be a little bit overpowering.

    Ghostbusters: Spirits Unleashed blends portions of prop-hunt gameplay with detective work and proton-pack blasting, making it an incredible multiplayer spooky blast for your timid hearted.

    Sign up for Our E-newsletter and retain updated on the most recent from HyperX. We guarantee to manage your details in line with our privacy plan.

    Continuing the helpful theme, Golf With Your Friends is your ticket to an excellent hoot with your pals. To the flipside, it's also a brilliant approach to tumble out with them.

    And the moment You begin delving into the caves and depths from the woodlands, the humanoid figures stalking your camps would be the least within your worries.

    ’s making procedure—and for free. Suddenly many of the individuals who didn’t wish to pony up $30 for PUBG

    I'm a major fan of Pathfinder, a robot with a grappling hook that lets you shut the hole, or escape from an enemy's clutches.

    io champ. The way to play: Generate a private space and customise the number of rounds, time for drawing and words for that game. As soon as you’re performed, send the invite link to your friends. If you’re the artist, try to draw the prompt provided.

    -design and style hero abilities, streamlined looting, and an intuitive pinging program that lets you converse with strangers without obtaining on-mic, or better sign to friends just wherever

    Conduct Interactive’s 4v1 multiplayer launch continues to be among the finest online horror games thus far, whether or not itsbloated perks procedure can make it a obstacle for new players to digest. Since launch,the game has actually been flooded with new Survivors and Killers to buy and scamper about as.

    This really is one of the best tank games ever manufactured. Environment of Tanks could possibly be a simulator with thorough game mechanics, but it really’s still accessible to new players.

    just captivated a happy-go-lucky crowd, nevertheless the game’s managed to stop most of the toxicity that plagues other multiplayer games. With friends, Rocket League

    Phasmophobia is like each haunted household Television set show you’ve ever viewed, only right here the ghosts are certainly actual and sure to have you fora midnight snack should you aren’t watchful.

    Apart from the otherworldly monsters, You can even encounter other players in PvP events, take a look at Dungeons, as well as investigate the whole world’s alternate dimensions named Slivers. There’s a whole lot to do below, and all for free.

    https://github.com/sagaming66
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-29 01:09 , Processed in 0.070732 second(s), 25 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表