【问题标题】:Selenium Webdriver using TestNG使用 TestNG 的 Selenium Webdriver
【发布时间】:2016-07-20 22:13:20
【问题描述】:

Selenium Webdriver:在此代码中,我的测试用例通过了一个测试电子邮件帐户,但对于第二个测试邮件它跳过了代码,它没有通过第二个测试电子邮件

package Packet;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NewTest {
    public WebDriver driver;
    // = new ChromeDriver();
    // System.setProperty("webdriver.chrome.driver","/Users/rohit/Desktop/chromedriver");
    public String baseurl = "http://google.com";

    @BeforeTest
    public void launchbrowser()

    {
        System.out.println("LaunchBrowser called...");
        System.setProperty("webdriver.chrome.driver", "/Users/rohit/Desktop/chromedriver");
        driver = new ChromeDriver();
        driver.get(baseurl);
    }
    // *[@id="gmail-sign-in"]//*[@id="gbw"]/div/div/div[1]/div[1]/a

    @BeforeMethod
    public void Beforemethod() {
        System.out.println("Hello Before Method");
    }

    @Test(priority = 0)
    public void GmailTap() {
        System.out.println("Test priority 0 called...");
        driver.findElement(By.xpath("//*[@id=\"gbw\"]/div/div/div[1]/div[1]/a")).click();
        // driver.findElement(By.xpath("//*[@id=\"gmail-sign-in\"]")).click();

        System.out.println("hello");
        driver.findElement(By.id("Email")).sendKeys("abc@gmail.com");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.HOURS);
        // Thread.sleep(8000);
        // driver.close();
        // findElement(By.cssSelector(""))
        driver.findElement(By.name("signIn")).click();
        // CharSequence charSequence = new
        boolean flag = true;
        while (flag) {
            try {
                driver.findElement(By.name("Passwd")).sendKeys("123456");
                System.out.println("Executed");
                flag = false;
            } catch (Exception e) {
            }
        }

        driver.findElement(By.id("signIn")).click();

        driver.findElement(By.xpath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
        driver.findElement(By.xpath("//*[@id=\"gb_71\"]")).click();

    }
    // _____________

    @Test(priority = 1)
    public void GmailTap2() {
        System.out.println("Test priority 1 called...");
        // driver.findElement(By.xpath("//*[@id=\"gbw\"]/div/div/div[1]/div[1]/a")).click();

        System.out.println("hello");
        driver.findElement(By.id("Email")).sendKeys("abc1@gmail.com");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.HOURS);

        driver.findElement(By.name("signIn")).click();
        boolean flag = true;
        while (flag) {
            try {
                driver.findElement(By.name("Passwd")).sendKeys("123456");
                System.out.println("Executed");
                flag = false;
            } catch (Exception e) {
            }
        }

        driver.findElement(By.id("signIn")).click();

        driver.findElement(By.xpath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
        driver.findElement(By.xpath("//*[@id=\"gb_71\"]")).click();

    }

    // ____________

    @AfterMethod
    public void aftersignout1() {
        driver.findElement(By.xpath("//*[@id=\"account-chooser-link\"]")).click();
        driver.findElement(By.xpath("//*[@id=\"account-chooser-add-account\"]")).click();
    }

    @AfterTest
    public void aftersignout() {
        System.out.println("aftersignOut called...");
        // New Accnt
        driver.close();
    }
    // *[@id="account-chooser-add-account"]

}

输出显示为:

LaunchBrowser called...
Starting ChromeDriver 2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b) on port 25912
Only local connections are allowed.
Hello Before Method
Test priority 0 called...
hello
Executed
Hello Before Method
Test priority 1 called...
[340.657][SEVERE]: Unable to receive message from renderer

【问题讨论】:

  • 为什么你一次又一次地为每个测试用例启动和销毁你的webdriver实例......尝试在@BeforeTest注解中启动一次并在@AfterTest注解处销毁它
  • @Noor ,我通过你的解释更新了我的代码,但它给了我一个错误,无法接收来自渲染器的消息。你能看看我的代码

标签: selenium-webdriver


【解决方案1】:

@AfterMethod 中的 hi 而不是 driver.close();请使用 driver.quit();

【讨论】:

  • 这对我不起作用。但是我更新了我的代码,但这没有用。
  • 遇到同样的问题或其他问题
  • 不同的问题为:LaunchBrowser 调用...正在端口 25912 上启动 ChromeDriver 2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b) 仅允许本地连接。调用方法测试优先级 0 之前的问候...您好执行调用方法测试优先级 1 之前的问候... [340.657][严重]:无法从渲染器接收消息
  • 跳过第二封测试邮件的测试用例
【解决方案2】:

终于得到了我的输出

package Packet;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NewTest {

     boolean flag = true;
    public WebDriver driver;
    public String baseurl = "http://google.com";



    @BeforeTest
     public void launchbrowser()

    {
        System.out.println("LaunchBrowser called...");
        System.setProperty("webdriver.chrome.driver", "/Users/rohit/Desktop/chromedriver");
        driver = new ChromeDriver();
        driver.get(baseurl);
    }


    @BeforeMethod
    public void Beforemethod() {
        System.out.println("Hello Before Method");
    }

    @Test(priority = 0)
    public void GmailTap() {
        System.out.println("Test priority 0 called...");
        driver.findElement(By.xpath("//*[@id=\"gbw\"]/div/div/div[1]/div[1]/a")).click();
        System.out.println("hello");
        driver.findElement(By.id("Email")).sendKeys("abc@gmail.com");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.HOURS);
        driver.findElement(By.name("signIn")).click();
        boolean flag = true;
        while (flag) {
            try {
                driver.findElement(By.name("Passwd")).sendKeys("123456");
                System.out.println("Executed");
                flag = false;
            } catch (Exception e) {
            }
        }

        driver.findElement(By.id("signIn")).click();
        driver.findElement(By.xpath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
        driver.findElement(By.xpath("//*[@id=\"gb_71\"]")).click();

    }





    @Test(priority = 1)
    public void GmailTap2() {
        System.out.println("Test priority 1 called...");
        System.out.println("hello");
        driver.findElement(By.id("Email")).sendKeys("abc@gmail.com");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.HOURS);
        driver.findElement(By.name("signIn")).click();
        boolean flag = true;
        while (flag) {
            try {
                driver.findElement(By.name("Passwd")).sendKeys("123456");
                System.out.println("Executed");
                flag = false;
            } catch (Exception e) {
            }
        }

        driver.findElement(By.id("signIn")).click();

        driver.findElement(By.xpath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
        driver.findElement(By.xpath("//*[@id=\"gb_71\"]")).click();

    }


    @AfterMethod
    public void aftersignout1() {

        if (flag)
         {
            driver.findElement(By.xpath("//*[@id=\"account-chooser-link\"]")).click();
         flag = false;
         }
        driver.findElement(By.xpath("//*[@id=\"account-chooser-add-account\"]")).click();
    }
    //*[@id="account-chooser-add-account"]
    @AfterTest
    public void aftersignout() {
        System.out.println("aftersignOut called...");
        // New Accnt`enter code here`
        driver.close();
    }
    }

【讨论】:

    【解决方案3】:

    下面的代码对我来说就像一个魅力

    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;
    
    public class NewTest {
        public WebDriver driver;
        public String baseurl = "http://google.com";
    
        @BeforeMethod
        public void launchbrowser()
    
        {
            System.out.println("LaunchBrowser called...");
            System.setProperty("webdriver.chrome.driver", "D:/your/path/to/Driver/chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.get(baseurl);
        }
    
        @Test(priority = 0)
        public void GmailTap() {
            System.out.println("Test priority 0 called...");
            driver.findElement(By.linkText("Gmail")).click();
    
            System.out.println("hello");
            driver.findElement(By.id("Email")).sendKeys("abc1@gmail.com");
            driver.findElement(By.id("next")).click();
    
            boolean flag = true;
    
            while (flag) {
    
                try {
                    driver.findElement(By.name("Passwd")).sendKeys("123456");
                    System.out.println("Executed");
                    flag = false;
                } catch (Exception e) {
                }
            }
    
            driver.findElement(By.id("signIn")).click();
    
            driver.findElement(By.xpath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
            driver.findElement(By.xpath("//*[@id=\"gb_71\"]")).click();
    
        }
    
        @Test(priority = 1)
        public void GmailTap2() {
            System.out.println("Test priority 1 called...");
            System.out.println("Test priority 0 called...");
            driver.findElement(By.linkText("Gmail")).click();
    
            System.out.println("hello");
            driver.findElement(By.id("Email")).sendKeys("abc1@gmail.com");
            driver.findElement(By.id("next")).click();
    
            boolean flag = true;
            while (flag) {
                try {
                    driver.findElement(By.name("Passwd")).sendKeys("123456");
                    System.out.println("Executed");
                    flag = false;
                } catch (Exception e) {
                }
            }
    
            driver.findElement(By.id("signIn")).click();
    
            WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions
                    .presenceOfElementLocated(By.xpath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")));
            myDynamicElement.click(); // driver.findElement(By.xpath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click();
                                        // driver.findElement(By.xpath("//*[@id=\"gb_71\"]")).click();
    
        }
    
        // ____________
    
        @AfterMethod
        public void aftersignout() {
            System.out.println("aftersignOut called..."); // New Accnt
    
            WebElement myDynamicElement = (new WebDriverWait(driver, 10))
                    .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"account-chooser-link\"]")));
            myDynamicElement.click();
    
            driver.findElement(By.xpath("//*[@id=\"account-chooser-add-account\"]")).click();
            driver.quit();
        } // *[@id="account-chooser-add-account"]
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 2016-04-25
      • 2017-01-17
      • 1970-01-01
      相关资源
      最近更新 更多