《手把手教你》系列技巧篇(四十三)-java+ selenium自动化测试-处理https 安全问题或者非信任站点-上篇(详解教程)

1.简介

    这一篇宏哥主要介绍webdriver在IE、Chrome和Firefox三个浏览器上处理不信任证书的情况,我们知道,有些网站打开是弹窗,SSL证书不可信任,但是你可以点击高级选项,继续打开不安全的链接。举例来说,想必大家都应该用过前几年的12306网站购票,点击新版购票,是不是会出现如下的界面。宏哥又找了一个https的页面,如下图所示:

2.三种浏览器如何处理不受信任的证书

三种浏览器访问网页,弹出证书不信任,需要点击下信任继续访问才行,多为访问https的网页。那么我们在做自动化测试的时候,如何跳过这一步骤,直接访问到我们需要的页面了,这个就是宏哥主要分享和讲解的如何在三大浏览器跳过这一步骤。

3.IE浏览器

3.1代码设计

3.2参考代码
package lessons;
 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;


/**
 * @author 北京-宏哥
 *
 *《手把手教你》系列技巧篇(四十三)-java+ selenium自动化测试-处理https 安全问题或者非信任站点-上篇(详解教程)
 *
 * 2021年11月11日
 */

public class TestHttps {
 
    public static void main(String[] args) throws Exception {
               
        // Set the driver path
        System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe"); 
        
        WebDriver driver = new InternetExplorerDriver();
        
        Thread.sleep(1000);
        driver.manage().window().maximize();
        driver.get("https://www.21xrx.com/");
        Thread.sleep(2000);
        String js = "javascript:document.getElementById('overridelink').click();";
        //To click on "Continue to this website (not recommended)." link to load original website.
        //driver.navigate().to("javascript:document.getElementById('overridelink').click()");
        driver.get(js);
        //JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
        //jsExecutor.executeScript(js);
        System.out.println(" 嘿嘿!宏哥,你已经成功跳过证书信任步骤啦!");
    }
 
}
3.3运行代码

1.运行代码,右键Run AS->Java Appliance,控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作,如下小视频所示:

4.Firefox浏览器

4.1代码设计

4.2参考代码
package lessons;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

/**
 * @author 北京-宏哥
 *
 *《手把手教你》系列技巧篇(四十三)-java+ selenium自动化测试-处理https 安全问题或者非信任站点(详解教程)
 *
 * 2021年11月11日
 */

public class TestHttps {
 
    public static void main(String[] args) throws Exception {
        
        System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe");  
         
        // 创建 firefox profile
        FirefoxProfile profile = new FirefoxProfile();
         
        // 把这项值设置为True,就是接受不可信任的证书
        profile.setAcceptUntrustedCertificates(true);
        
        FirefoxOptions firefoxOptions = new FirefoxOptions(); 
        firefoxOptions.setProfile(profile); 
         
        // 打开一个带上门设置好profile的火狐浏览器
        WebDriver driver = new FirefoxDriver(firefoxOptions);
        //driver.setProfile(profile);
        driver.manage().window().maximize();
        driver.get("https://www.21xrx.com/");
        System.out.println(" 嘿嘿!宏哥,你已经成功跳过证书信任步骤啦!");
    }
 
}
4.3运行代码

1.运行代码,右键Run AS->Java Appliance,控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作,如下小视频所示:

5.小结

5.1IE浏览器遇到问题及解决办法

1.运行IE浏览器报错:

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

 解决办法:

有的小伙伴或者童鞋们可能觉得是版本的问题,宏哥第一想法也是这个问题,但是又想了想,以前可以运行现在连浏览器的启动不了,确定不是版本问题,而是由其他原因引起的。这是因为没有关闭IE浏览器的保护模式。当运行测试用例后出现类似以下内容的错误:

Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer.
Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

应该就是IE浏览器的保护模式未关闭。

  

在这里可以关闭保护模式。需要注意的是,我们访问的站点是哪个区域的,就要把那个区域的保护模式观点。(一般来说我都是关全部)

而针对IE10及以上版本,我们需要关闭“增强保护模式”

      

PS:  请注意这里的选项是“重启计算机后生效”!而针对IE11,我们需要进一步修改注册表。(Run->regedit->Enter)

    

如果FeatureControl下没有FEATURE_BFCACHE,就以FEATURE_BFCACHE为名new一个key!并在其下创建一个DWORD,取名为:iexplore.exe,value值为0。

另外,别忘了一件事情,就是IE的缩放选项。请设置缩放选项为100%,否则可能无法定位页面元素。

2.IE以前遇到这种问题代码这么写,就可以现在就不行了,所以宏哥换了一种方式,利用前边学习过的JavaScript执行知识进行解决。

package lessons;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * @author 北京-宏哥
 *
 *《手把手教你》系列技巧篇(四十三)-java+ selenium自动化测试-处理https 安全问题或者非信任站点(详解教程)
 *
 * 2021年11月11日
 */

public class TestHttps {
 
    public static void main(String[] args) throws Exception {
                 
        // Create object of DesiredCapabilities class
        DesiredCapabilities cap=DesiredCapabilities.internetExplorer();
        cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); 
        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); 
        cap.setJavascriptEnabled(true); 
        
        // Set the driver path
        System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe"); 
        
        // Open browser with capability
        WebDriver driver=new InternetExplorerDriver(cap);
        driver.manage().window().maximize();
        driver.get("https://www.21xrx.com/");
        System.out.println(" 嘿嘿!宏哥,你已经成功跳过证书信任步骤啦!");
    }
 
}

3.也许有的小伙伴或者童鞋们,发现使用宏哥的代码也不成功,那是因为你没有将所有的安全保护模式关闭,解决办法:参考宏哥知识点1,将所有安全保护模式关闭,再次运行代码就成功了。

5.2Firefox浏览器遇到问题及解决办法

1.Firefox以前遇到这种问题代码这么写,就可以现在就不行了,所以宏哥也换了一种方式。

package lessons;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * @author 北京-宏哥
 *
 *《手把手教你》系列技巧篇(四十三)-java+ selenium自动化测试-处理https 安全问题或者非信任站点(详解教程)
 *
 * 2021年11月11日
 */

public class TestHttps {
 
    public static void main(String[] args) throws Exception {
                 
        System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe");  
         
        // 创建 firefox profile
        FirefoxProfile profile = new FirefoxProfile();
         
        // 把这项值设置为True,就是接受不可信任的证书
        profile.setAcceptUntrustedCertificates(true);
         
        // 打开一个带上门设置好profile的火狐浏览器
        WebDriver driver = new FirefoxDriver(profile);
        
        driver.manage().window().maximize();
        driver.get("https://www.21xrx.com/");
        System.out.println(" 嘿嘿!宏哥,你已经成功跳过证书信任步骤啦!");
    }
 
}

但是代码报错如下图所示:

 解决办法:宏哥换了一种写法,查看4.2的参考代码

    每天学习一点,今后必成大神-

往期推荐(由于跳转参数丢失了,所有建议选中要访问的右键,在新标签页中打开链接即可访问)或者微信搜索: 北京宏哥  公众号提前解锁更多干货。

Appium自动化系列,耗时80天打造的从搭建环境到实际应用精品教程测试

Python接口自动化测试教程,熬夜87天整理出这一份上万字的超全学习指南

Python+Selenium自动化系列,通宵700天从无到有搭建一个自动化测试框架

Java+Selenium自动化系列,仿照Python趁热打铁呕心沥血317天搭建价值好几K的自动化测试框架

Jmeter工具从基础->进阶->高级,费时2年多整理出这一份全网超详细的入门到精通教程

Fiddler工具从基础->进阶->高级,费时100多天吐血整理出这一份全网超详细的入门到精通教程

Pycharm工具基础使用教程

  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值