Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
seveniruby committed Dec 1, 2021
1 parent a72a049 commit ad154ea
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 72 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
<name>appcrawler</name>
<description>app crawler for automation testing, 全平台遍历测试工具,霍格沃兹测试开发学社出品,交流论坛 https://ceshiren.com/c/opensource/appcrawler</description>

<!-- <parent>-->
<!-- <groupId>com.ceshiren.appcrawler</groupId>-->
<!-- <artifactId>appcrawler-modules</artifactId>-->
<!-- <version>2.7.2</version>-->
<!-- </parent>-->

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
Expand Down
40 changes: 20 additions & 20 deletions src/main/scala/com/ceshiren/appcrawler/core/Crawler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Crawler {
var conf = new CrawlerConf()

/** 存放插件类 */
val pluginClasses = ListBuffer[Plugin]()
val pluginClasses: ListBuffer[Plugin] = ListBuffer[Plugin]()

var store: URIElementStore = _

Expand All @@ -53,7 +53,7 @@ class Crawler {
signals.append(1)
private val startTime = new Date().getTime

val urlStack = mutable.Stack[String]()
val urlStack: mutable.Stack[String] = mutable.Stack[String]()

protected val backDistance = new DataRecord()
val appNameRecord = new DataRecord()
Expand Down Expand Up @@ -241,12 +241,12 @@ class Crawler {
def restart(): Unit = {
if (conf.beforeRestart != null) {
log.info("execute shell on restart")
conf.beforeRestart.foreach(DynamicEval.dsl(_))
conf.beforeRestart.foreach(DynamicEval.dsl)
}
log.info("restart appium")
conf.capability ++= Map("app" -> "")
conf.capability ++= Map("dontStopAppOnReset" -> "true")
conf.capability ++= Map("noReset" -> "true")
conf.capability ++= mutable.Map("app" -> "")
conf.capability ++= mutable.Map("dontStopAppOnReset" -> "true")
conf.capability ++= mutable.Map("noReset" -> "true")
setupCrawler()
waitAppLoaded()
firstRefresh()
Expand Down Expand Up @@ -442,9 +442,9 @@ class Crawler {

//url白名单, 第一次进入了白名单的范围, 就始终在白名单中. 不然就算不在白名单中也得遍历.
//上层是白名单, 当前不是白名单才需要返回
if (conf.urlWhiteList.size > 0
&& conf.urlWhiteList.filter(urlStack.head.matches(_)).isEmpty
&& conf.urlWhiteList.filter(urlStack.tail.headOption.getOrElse("").matches(_)).nonEmpty) {
if (conf.urlWhiteList.nonEmpty
&& !conf.urlWhiteList.exists(urlStack.head.matches(_))
&& conf.urlWhiteList.exists(urlStack.tail.headOption.getOrElse("").matches(_))) {
log.warn(s"${urlStack.head} not in urlWhiteList should return")
result = true
}
Expand Down Expand Up @@ -834,18 +834,18 @@ class Crawler {
} else {
None
}
}).filter(_.nonEmpty).map(someStep => {
}).filter(_.nonEmpty).flatMap(someStep => {
getURIElementsByStep(someStep.get).map(e => {
e.setAction(someStep.get.getAction())
e
})
}).flatten.distinct.toList
}).distinct.toList
//排序,depth小的在前面,但是带有back action的控件排在最后
.sortWith(_.getDepth < _.getDepth)
.sortWith(_.getAction.indexOf("Back") < _.getAction.indexOf("Back"))
//追加到backButton后面,depth小的放前面
.map(e => {
if (conf.backButton.filter(_.getXPath() == e.getXpath).size == 0 && e.getAction != backAction) {
if (!conf.backButton.exists(_.getXPath() == e.getXpath) && e.getAction != backAction) {
log.info(s"find new back button from history ${e}")
conf.backButton.append(model.Step(xpath = e.getXpath, action = e.getAction))
}
Expand Down Expand Up @@ -890,7 +890,7 @@ class Crawler {
//找到可能的关闭按钮, 取第一个可用的关闭按钮
log.trace(conf.backButton)
conf.backButton.flatMap(step => getURIElementsByStep(step)).headOption match {
case Some(backElement) if appNameRecord.isDiff() == false => {
case Some(backElement) if !appNameRecord.isDiff() => {

if (isEndlessLoop()) {
return Some(getEventElement("Back"))
Expand All @@ -905,7 +905,7 @@ class Crawler {
}

// 通过配置文件设置的Xpath找到返回键,将其真实Xpath添加进List
if (conf.backButton.filter(_.getXPath() == backElement.getXpath).size == 0 && backElement.getAction != backAction) {
if (!conf.backButton.exists(_.getXPath() == backElement.getXpath) && backElement.getAction != backAction) {
log.info(s"find new back button from configuration file ${backElement}")
conf.backButton.append(model.Step(xpath = backElement.getXpath, action = backElement.getAction))
}
Expand All @@ -916,7 +916,7 @@ class Crawler {
log.info("can't find backButton button from config")
log.info("find backButton from history")
getPredictBackNodes().headOption match {
case Some(backElement) if appNameRecord.isDiff() == false => {
case Some(backElement) if !appNameRecord.isDiff() => {
//app相同并且找到back控件才点击. 否则就默认back
//todo: bug hierarchy click出现
if (backElement.getAction.isEmpty) {
Expand All @@ -937,7 +937,7 @@ class Crawler {
}


def fixElementAction(element: URIElement) = {
def fixElementAction(element: URIElement): Unit = {
pluginClasses.foreach(c => c.fixElementAction(element))
}

Expand Down Expand Up @@ -997,7 +997,7 @@ class Crawler {

//查找正常的元素
if (nextElement.isEmpty) {
nextElement = getAvailableElement(driver.page, true)
nextElement = getAvailableElement(driver.page, skipSmall = true)
}

if (nextElement.isEmpty) {
Expand Down Expand Up @@ -1052,10 +1052,10 @@ class Crawler {
log.info(s"current index = ${store.getClickedElementsList.size - 1}")
log.info(s"current xpath = ${element.getXpath}")
log.info(s"current action = ${element.getAction}")
log.info(s"current element = ${element.elementUri}")
log.info(s"current element = ${element.elementUri()}")
log.info(s"current url = ${element.getUrl}")
log.info(s"current tag path = ${element.getAncestor()}")
log.info(s"current file name = ${element.elementUri.take(100)}")
log.info(s"current file name = ${element.elementUri().take(100)}")

store.saveReqHash(contentHash.last().toString)
store.saveReqDom(driver.page.toXML)
Expand Down Expand Up @@ -1216,7 +1216,7 @@ class Crawler {
def saveScreen(force: Boolean = false): Unit = {
//如果是schema相同. 界面基本不变. 那么就跳过截图加快速度.
val originPath = getBasePathName() + ".clicked.png"
if (pluginClasses.map(p => p.screenshot(originPath)).contains(true)) {
if (pluginClasses.exists(p => p.screenshot(originPath))) {
return
}
if (conf.screenshot || force) {
Expand Down
42 changes: 21 additions & 21 deletions src/main/scala/com/ceshiren/appcrawler/core/CrawlerConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class CrawlerConf {
/** sikuli的数据 */
//var sikuliImages=""
//todo: 通过数据驱动,支持多设备
val capabilityDescription = "appium的capability通用配置,其中automationName代表自动化的驱动引擎," +
val capabilityDescription: String = "appium的capability通用配置,其中automationName代表自动化的驱动引擎," +
"除了支持appium的所有引擎外,额外增加了adb和selenium的支持"
var capability = Map[String, Any](
var capability: Map[String, Any] = Map[String, Any](
//默认不清空数据,防止有人用于微信和qq
"appPackage" -> "io.appium.android.apis",
"appActivity" -> ".ApiDemos",
Expand All @@ -43,20 +43,20 @@ class CrawlerConf {

//测试用例
val testcaseDescription = "测试用例设置,用于遍历开始之前的一些前置操作,比如自动登录"
var testcase = ReactTestCase(
var testcase: ReactTestCase = ReactTestCase(
name = "AppCrawler TestCase",
steps = List[Step](
Step(xpath = "/*/*", action = "Thread.sleep(1000)")
)
)

val maxTimeDescription = "最大运行时间"
var maxTime = 3600 * 3
var maxTime: Int = 3600 * 3
val maxDepthDescription = "默认的最大深度10, 结合baseUrl可很好的控制遍历的范围"
var maxDepth = 10

val selectedListDescription = "默认遍历列表,只有出现在这个列表里的控件范围才会被遍历"
var selectedList = ListBuffer[Step](
var selectedList: ListBuffer[Step] = ListBuffer[Step](
Step(xpath = "//*[contains(name(), 'Button')]"),
//android专属
Step(xpath = "//*[contains(name(), 'Text') and @clickable='true' and string-length(@text)<10]"),
Expand All @@ -77,26 +77,26 @@ class CrawlerConf {
)

val triggerActionsDescription = "在遍历过程中需要随时处理的一些操作,比如弹框、登录等"
var triggerActions = ListBuffer[Step](
var triggerActions: ListBuffer[Step] = ListBuffer[Step](
Step(xpath = "permission_allow_button", times = 3),
Step(xpath = "允许", times = 3)
)

val blackListDescription = "黑名单列表 matches风格, 默认排除内容包含2个数字的控件"
var blackList = ListBuffer[Step](
var blackList: ListBuffer[Step] = ListBuffer[Step](
Step(xpath = ".*[0-9]{2}.*"),
Step(xpath = "Get Music")
)
val firstListDescription = "优先遍历列表,同时出现在selectedList与firstList中的控件会被优先遍历"
var firstList = ListBuffer[Step](
var firstList: ListBuffer[Step] = ListBuffer[Step](
)
val lastListDescription = "最后遍历列表,同时出现在selectedList与lastList中的控件会被最后遍历"
var lastList = ListBuffer[Step](
var lastList: ListBuffer[Step] = ListBuffer[Step](
Step(xpath = "//*[@selected='true']/..//*"),
Step(xpath = "//*[@selected='true']/../..//*")
)
val backButtonDescription = "后退按钮列表,默认在所有控件遍历完成后,才会最后点击后退按钮。目前具备了自动判断返回按钮的能力,默认不需要配置"
var backButton = ListBuffer[Step](
var backButton: ListBuffer[Step] = ListBuffer[Step](
Step(xpath = "Navigate up")
)

Expand All @@ -117,13 +117,13 @@ class CrawlerConf {
val findByDescription = "默认生成控件唯一定位符的表达式风格,可选项 default|android|id|xpath,默认会自动判断是否使用android定位或者ios定位"
var findBy = "xpath"
val baseUrlDescription = "设置一个起始点,从这个起始点开始计算深度,比如默认从登录后的界面开始计算"
var baseUrl = List[String]()
var baseUrl: List[String] = List[String]()
val appWhiteListDescription = "app白名单,允许在这些app里进行遍历"
var appWhiteList = ListBuffer[String]()
var appWhiteList: ListBuffer[String] = ListBuffer[String]()
val urlBlackListDescription = "url黑名单,用于排除某些页面的遍历"
var urlBlackList = ListBuffer[String]()
var urlBlackList: ListBuffer[String] = ListBuffer[String]()
val urlWhiteListDescription = "url白名单,仅在这些界面内遍历"
var urlWhiteList = ListBuffer[String]()
var urlWhiteList: ListBuffer[String] = ListBuffer[String]()

val beforeRestartDescription = "在重启session之前做的事情"
var beforeRestart: ListBuffer[String] = ListBuffer[String]()
Expand Down Expand Up @@ -157,18 +157,18 @@ class CrawlerConf {
"id", "name", "tag", "class"
)
val tagLimitDescription = "设置部分相似控件的最大遍历次数"
var tagLimit = ListBuffer[Step](
var tagLimit: ListBuffer[Step] = ListBuffer[Step](
//特殊的按钮,可以一直被遍历
Step(xpath = "确定", times = 1000),
Step(xpath = "取消", times = 1000),
Step(xpath = "share_comment_guide_btn_name", times = 1000),
Step(xpath = "//*[contains(@class, 'List')]//*", times = 2)
)
val assertGlobalDescription = "全局断言"
var assertGlobal = List[Step]()
var assertGlobal: List[Step] = List[Step]()

val suiteNameDescription = "报告中的测试套件名字可以由列表内的控件内容替换,增强报告中关键界面的辨识度"
var suiteName = List[String](
var suiteName: List[String] = List[String](
"//*[@selected='true']//android.widget.TextView/@text"
)
val screenshotDescription = "是否截图"
Expand All @@ -184,13 +184,13 @@ class CrawlerConf {

val pluginListDescription = "插件列表,暂时禁用,太高级了,很多人不会用"

val Description =
val Description: String =
"""
|在selectedList firstList lastList等很多配置中,需要填充的是测试步骤Step类型
|Step类型由given(满足条件)when(条件满足的行为)then(断言)三部分组成
|Step可以简化为xpath(定位表达式,支持xpath 正则 包含关系)与action(点击 输入等行为)
|""".stripMargin.replace("\n", "")
var pluginList = List[String]()
var pluginList: List[String] = List[String]()

def save(path: String): Unit = {

Expand All @@ -212,14 +212,14 @@ class CrawlerConf {
println(mapper.writeValueAsString(this))
}

def toJson(): String = {
def toJson: String = {
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this)

}

def toYaml(): String = {
def toYaml: String = {
val mapper = new ObjectMapper(new YAMLFactory())
mapper.registerModule(DefaultScalaModule)
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract class ReactWebDriver {
case len if len > 1 => {
log.warn(s"find count ${v.size}, you should check your dom file")
//有些公司可能存在重名id
arr.foreach(log.info)
arr.foreach(log.debug)
log.warn("just use the first one")
return arr.head
}
Expand All @@ -70,9 +70,9 @@ abstract class ReactWebDriver {

def backApp(): Unit = {}

def launchApp()
def launchApp(): Unit

def getPageSource(): String
def getPageSource: String


//todo: 有的时候会出现极少内容的page source
Expand Down Expand Up @@ -265,19 +265,19 @@ abstract class ReactWebDriver {
//支持宽松查找,自动刷新查找,自动滚动查找
def getNodeListByKeyWithRetry(key: String): List[Map[String, Any]] = {
var array = getNodeListByKey(key)
if (array.size == 0) {
if (array.isEmpty) {
getPageSourceWithRetry()
log.debug("retry 1")
array = getNodeListByKey(key)
}

if (array.size == 0) {
if (array.isEmpty) {
getPageSourceWithRetry()
log.debug("retry 2")
array = getNodeListByKey(key)
}

if (array.size == 0) {
if (array.isEmpty) {
getPageSourceWithRetry()
log.debug("retry 3")
array = getNodeListByKey(key)
Expand Down
Loading

0 comments on commit ad154ea

Please sign in to comment.