python3_map()函数

坑爹python3改动之map()函数

做自动化测试,获取并校验下拉列表中所有值,由于实在不想先写个空列表,再来个for循环全部加到列表中,就想到了map()函数,折腾半天,各种报错,墙里墙外翻半天,终于找到跟我一样入坑的人。

map()函数,python3改动

在python2中,以下代码可输出一个列表[1, 2, 3, 4, 5]

1
2
a = map(lambda x:x, [1, 2, 3, 4, 5])
print(a)

但是python3,这玩意只能输出个map对象:<map object at 0x00000278CB45D4E0>

遇到这玩意也很多次了,直接加list:

1
2
a = list(map(lambda x: x, [1, 2, 3, 4, 5]))
print(a)

这样,在python3就可以输出[1, 2, 3, 4, 5]

附自动化测试比较下拉列表值是否符合预期示例代码(百度新闻高级设置-显示条数为例):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import unittest
import time

class News_Baidu(unittest.TestCase):

def setUp(self):
#打开浏览器
self.brower = webdriver.Chrome()

def test_Checklist(self):
#打开百度新闻页面
self.brower.get("http://news.baidu.com/")
#定位“高级设置”,并点击
self.brower.find_element_by_link_text("高级搜索").click()
#定位“搜索结果显示条数”
select_element = Select(self.brower.find_element_by_name('rn'))
#获取下拉列表所有元素对象
select_options = select_element.options
#声明一个期望下拉列表值的列表(百度也是够坑的,值前面还有一个空格)
expect_optionlist = [' 每页显示10条', ' 每页显示20条', ' 每页显示50条']
#获取实际的下拉列表值的列表
actual_optionlist = list(map(lambda option:option.text, select_options))
#断言结果是否适合期望
self.assertListEqual(actual_optionlist, expect_optionlist)

def tearDown(self):
#休眠
time.sleep(5)
#退出浏览器
self.brower.quit()

if __name__ == '__main__':
unittest.main()


文章目录
|