测试运行后IE浏览器没有关闭

时间:2018-01-13 04:19:37

标签: selenium selenium-webdriver testng

我使用TestNG运行Selenium代码进行跨浏览器测试,进程后Chrome浏览器关闭但IE运行后没有关闭。 以下是浏览器测试的代码:

package learningTestNGVideo;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class CrossBrowserTesting {
    //global variable WebDriver
    public WebDriver driver;
    @Parameters("browser")
    @BeforeClass
    public void LaunchBrowser(String browser){

        if(browser.equalsIgnoreCase("IE")){
            System.setProperty("webdriver.ie.driver","E:\\Selenium2018\\IEDriverServer_x64_3.8.0\\IEDriverServer.exe");
            driver= new InternetExplorerDriver();
        }
        else if(browser.equalsIgnoreCase("Chrome")){
            System.setProperty("webdriver.chrome.driver","E:\\Selenium2018\\setup\\chromedriver_win32\\chromedriver.exe");
            driver= new ChromeDriver();

        }
        else if(browser.equalsIgnoreCase("firefox")){
            System.setProperty("webdriver.gecko.driver","E:\\Selenium2018\\geckodriver-v0.19.1-win64\\geckodriver.exe");

            driver= new FirefoxDriver();
        }

    driver.get("http://automationpractice.com/index.php");
    }

    @Test
    public void ClickonSignIn(){

        driver.findElement(By.xpath(".//*[@id='header']/div[2]/div/div/nav/div[1]/a")).click();
        System.out.println("Successfull");
        }
    @AfterMethod
    public void CloseBrowser() throws IOException{
        driver.close();
    }

}

以下是我为关闭浏览器方法

获取的错误
 org.openqa.selenium.NoSuchWindowException: Unable to get browser
    Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z'
    System info: host: '####', ip: '####', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_144'
    Driver info: org.openqa.selenium.ie.InternetExplorerDriver
    Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:25599/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
    Session ID: e346d8bb-4875-4567-902e-428e73f34013
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

这是xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" parallel="tests">
  <test name="Test-IE">
    <parameter name="browser" value="IE"></parameter>
    <classes>
      <class name="learningTestNGVideo.CrossBrowserTesting" />
    </classes>
  </test>
  <!-- Test -->
  <test name="Test-Firefox">
    <parameter name="browser" value="firefox"></parameter>
    <classes>
      <class name="learningTestNGVideo.CrossBrowserTesting" />
    </classes>
  </test>
  <test name="Test-Chrome">
    <parameter name="browser" value="chrome"></parameter>
    <classes>
      <class name="learningTestNGVideo.CrossBrowserTesting" />
    </classes>
  </test>
</suite>
<!-- Suite -->

1 个答案:

答案 0 :(得分:0)

此处的问题似乎与您的IE的安全设置有关。应将所有安全区域设置为相同的保护模式设置。我发现将Local Intranet区域的Enable Protected Mode设置为true可以解决这个问题。

按Alt键调出IE11菜单栏。 选择工具&gt; Internet选项并转到“安全”选项卡。 选择每个区域(Int​​ernet,本地Intranet,受信任的站点,受限制的站点),然后选中“启用保护模式”复选框。