<i id='JKQMP'><tr id='JKQMP'><dt id='JKQMP'><q id='JKQMP'><span id='JKQMP'><b id='JKQMP'><form id='JKQMP'><ins id='JKQMP'></ins><ul id='JKQMP'></ul><sub id='JKQMP'></sub></form><legend id='JKQMP'></legend><bdo id='JKQMP'><pre id='JKQMP'><center id='JKQMP'></center></pre></bdo></b><th id='JKQMP'></th></span></q></dt></tr></i><div id='JKQMP'><tfoot id='JKQMP'></tfoot><dl id='JKQMP'><fieldset id='JKQMP'></fieldset></dl></div>
<legend id='JKQMP'><style id='JKQMP'><dir id='JKQMP'><q id='JKQMP'></q></dir></style></legend>

  1. <tfoot id='JKQMP'></tfoot>

      <small id='JKQMP'></small><noframes id='JKQMP'>

      • <bdo id='JKQMP'></bdo><ul id='JKQMP'></ul>
    1. Selenium Webdriver:如何在同一个窗口中一个接一个地运行多个测试?

      Selenium Webdriver: How do I run multiple tests, one after the other in the same window?(Selenium Webdriver:如何在同一个窗口中一个接一个地运行多个测试?)
        <legend id='psdde'><style id='psdde'><dir id='psdde'><q id='psdde'></q></dir></style></legend>

        1. <i id='psdde'><tr id='psdde'><dt id='psdde'><q id='psdde'><span id='psdde'><b id='psdde'><form id='psdde'><ins id='psdde'></ins><ul id='psdde'></ul><sub id='psdde'></sub></form><legend id='psdde'></legend><bdo id='psdde'><pre id='psdde'><center id='psdde'></center></pre></bdo></b><th id='psdde'></th></span></q></dt></tr></i><div id='psdde'><tfoot id='psdde'></tfoot><dl id='psdde'><fieldset id='psdde'></fieldset></dl></div>
            <tbody id='psdde'></tbody>
              <bdo id='psdde'></bdo><ul id='psdde'></ul>
              • <tfoot id='psdde'></tfoot>
              • <small id='psdde'></small><noframes id='psdde'>

                本文介绍了Selenium Webdriver:如何在同一个窗口中一个接一个地运行多个测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我的目标是让一系列测试一个接一个地运行.我想让一个登录"脚本让用户登录,然后下面的脚本开始在同一个窗口/驱动程序中继续.我正在使用 TestNG,所以如果有帮助,我的测试套件会在 testng.xml 文件中设置.

                My goal is to have a series of tests run one after the other. I would like to have a "login" script log the user in and then the following scripts kick off continuing in the same window/driver. I'm using TestNG so my test suite is setup in the testng.xml file if that helps.

                public class LoginScript {
                String username, password, siteid;
                private WebDriver driver;
                private boolean acceptNextAlert = true;
                private StringBuffer verificationErrors = new StringBuffer();
                static Logger log = Logger.getLogger(LoginScript.class);
                
                
                @BeforeSuite (alwaysRun=true)
                @Parameters({ "url","username","password","site" })
                
                public void setUp(String env, String user, String pwd, String ste) throws Exception {
                username=user;
                password=pwd;
                siteid=ste;
                
                driver = new FirefoxDriver();
                driver.get(env);
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                }
                
                @Test
                public void testLoginScript() throws Exception {
                //Maximize window
                driver.manage().window().maximize();
                
                //Login
                driver.findElement(By.id("TBSiteID")).clear();
                driver.findElement(By.id("TBSiteID")).sendKeys(siteid);
                driver.findElement(By.id("TBUserName")).clear();
                driver.findElement(By.id("TBUserName")).sendKeys(username);
                driver.findElement(By.name("TBPassword")).clear();
                driver.findElement(By.name("TBPassword")).sendKeys(password);
                driver.findElement(By.name("Login")).click();
                Thread.sleep(2000);
                log.info("Found requested site");
                
                }
                
                 @AfterSuite
                 public void tearDown() throws Exception {
                //driver.quit();
                String verificationErrorString = verificationErrors.toString();
                if (!"".equals(verificationErrorString)) {
                  fail(verificationErrorString);
                 }
                 }
                
                private boolean isElementPresent(By by) {
                try {
                  driver.findElement(by);
                  return true;
                } catch (NoSuchElementException e) {
                  return false;
                 }
                }
                
                private boolean isAlertPresent() {
                try {
                  driver.switchTo().alert();
                  return true;
                } catch (NoAlertPresentException e) {
                  return false;
                  }
                 }
                
                 private String closeAlertAndGetItsText() {
                  try {
                  Alert alert = driver.switchTo().alert();
                  String alertText = alert.getText();
                  if (acceptNextAlert) {
                    alert.accept();
                  } else {
                    alert.dismiss();
                  }
                  return alertText;
                } finally {
                  acceptNextAlert = true;
                }
                }
                }
                

                我想运行的下一个脚本:

                Next script that I would like to run:

                public class AddNormalEE {
                String username, password, siteid;
                private WebDriver driver;
                private boolean acceptNextAlert = true;
                 private StringBuffer verificationErrors = new StringBuffer();
                static Logger log = Logger.getLogger(AddNormalEE.class);
                
                
                @BeforeSuite (alwaysRun=true)
                @Parameters({ "url","username","password","site" })
                
                public void setUp(String env, String user, String pwd, String ste) throws Exception {
                username=user;
                 password=pwd;
                siteid=ste;
                
                    //driver = new FirefoxDriver();
                  //driver.get(env);
                  //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                }
                
                
                @Test
                public void testAddNormalEE() throws Exception {
                //Maximize window
                //driver.manage().window().maximize();
                
                
                
                @AfterSuite
                public void tearDown() throws Exception {
                driver.quit();
                String verificationErrorString = verificationErrors.toString();
                if (!"".equals(verificationErrorString)) {
                  fail(verificationErrorString);
                }
                }
                
                private boolean isElementPresent(By by) {
                try {
                  driver.findElement(by);
                  return true;
                  } catch (NoSuchElementException e) {
                    return false;
                  }
                }
                
                private boolean isAlertPresent() {
                try {
                  driver.switchTo().alert();
                  return true;
                } catch (NoAlertPresentException e) {
                  return false;
                }
                }
                
                private String closeAlertAndGetItsText() {
                try {
                  Alert alert = driver.switchTo().alert();
                  String alertText = alert.getText();
                  if (acceptNextAlert) {
                    alert.accept();
                  } else {
                    alert.dismiss();
                  }
                  return alertText;
                } finally {
                  acceptNextAlert = true;
                }
                 }
                }  
                

                推荐答案

                如果你的测试是依赖的,你可以把它们放在同一个类中,并在method2上定义dependsOnMethod={method1},这样可以保证顺序.如果它在不同的类之间,您可以考虑从 LoginScript 类扩展您的 AddNormalEE 类.

                If your tests are dependent, you can put them in the same class with dependsOnMethod={method1} defined on method2 so that the order is gauranteed. If it is between different classes, you might consider extending your AddNormalEE class from the LoginScript class.

                要在同一个浏览器中运行测试,您的驱动程序实例需要在您的类之间共享,或者它必须在您的所有@Tests 中都相同.要么将其设为静态,要么考虑使用 threadlocal webdriver 变量,以防您计划有一天并行运行.在上述情况下,您还可以在 loginScript 中使用 getDriver() 方法,如果需要避免静态,则该方法将驱动程序对象返回给 AddNormalEE 类.

                To run tests in the same browser, your driver instance needs to be shared between your classes or it has to be the same in all your @Tests. Either make it static or consider having a threadlocal webdriver variable in case you plan to run parallely some day. In the above case, you can also have a method getDriver() in your loginScript which returns the driver object to the AddNormalEE class if static needs to be avoided.

                作为一般做法,最好进行独立测试.您可以利用并行运行来克服独立测试的时间问题.将登录作为一种方法而不是测试,因为我们没有根据上面的代码断言任何行为.如果我正在测试登录,我将进行单独的测试来仅测试登录功能.

                As a general practice, it is good to have independent tests. You can make use of parallel runs to overcome the time issue with independent tests. Making login as a method and not a test since we are not asserting any behavior as per your code above. If I am testing login, I would have separate tests that test login functionality only.

                这篇关于Selenium Webdriver:如何在同一个窗口中一个接一个地运行多个测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Maven JAVA_HOME environment variable is not defined correctly, but it is(Maven JAVA_HOME 环境变量未正确定义,但它是)
                Java System Environment Variable(Java 系统环境变量)
                Java -classpath option(Java -classpath 选项)
                Read environment variable in SpringBoot(在 SpringBoot 中读取环境变量)
                Tomcat 8 - context.xml use Environment Variable in Datasource(Tomcat 8 - context.xml 在数据源中使用环境变量)
                Issue with JAVA_HOME(JAVA_HOME 的问题)

                  <small id='0ZLLB'></small><noframes id='0ZLLB'>

                  <tfoot id='0ZLLB'></tfoot>
                • <legend id='0ZLLB'><style id='0ZLLB'><dir id='0ZLLB'><q id='0ZLLB'></q></dir></style></legend>

                    • <bdo id='0ZLLB'></bdo><ul id='0ZLLB'></ul>

                            <tbody id='0ZLLB'></tbody>

                          <i id='0ZLLB'><tr id='0ZLLB'><dt id='0ZLLB'><q id='0ZLLB'><span id='0ZLLB'><b id='0ZLLB'><form id='0ZLLB'><ins id='0ZLLB'></ins><ul id='0ZLLB'></ul><sub id='0ZLLB'></sub></form><legend id='0ZLLB'></legend><bdo id='0ZLLB'><pre id='0ZLLB'><center id='0ZLLB'></center></pre></bdo></b><th id='0ZLLB'></th></span></q></dt></tr></i><div id='0ZLLB'><tfoot id='0ZLLB'></tfoot><dl id='0ZLLB'><fieldset id='0ZLLB'></fieldset></dl></div>