Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
Vue-WebDriver
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
高源
Vue-WebDriver
Commits
49a6ae73
Commit
49a6ae73
authored
Nov 18, 2023
by
施晓雨
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新结构
parent
07480c73
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
315 deletions
+24
-315
sdk-table.js
src/sdk-table.js
+24
-315
No files found.
src/sdk-table.js
View file @
49a6ae73
...
@@ -16,8 +16,8 @@ function table () {
...
@@ -16,8 +16,8 @@ function table () {
*/
*/
this
.
newCell
=
function
(
xpath
)
{
this
.
newCell
=
function
(
xpath
)
{
_isVerified
=
false
;
//加入了
_isVerified
=
false
;
//加入了
let
celld
=
new
cell
(
xpath
);
let
newCell
=
new
cell
(
xpath
);
this
.
cells
.
push
(
celld
);
this
.
cells
.
push
(
newCell
);
}
}
/**
/**
* 根据传入的cell进行验证,如果验证成功,xpath自动变化
* 根据传入的cell进行验证,如果验证成功,xpath自动变化
...
@@ -44,36 +44,31 @@ function table () {
...
@@ -44,36 +44,31 @@ function table () {
while
(
tableXpath
.
length
>
0
)
{
while
(
tableXpath
.
length
>
0
)
{
this
.
cells
.
forEach
(
cell
=>
cell
.
updateRelativePath
(
tableXpath
));
//先对所有的Cell更新下相对路径
this
.
cells
.
forEach
(
cell
=>
cell
.
updateRelativePath
(
tableXpath
));
//先对所有的Cell更新下相对路径
if
(
this
.
cells
.
every
(
cell
=>
cell
.
isExist
(
cell
)))
break
;
//如果每个cell都存在,成功
if
(
this
.
cells
.
every
(
cell
=>
cell
.
isExist
()))
break
;
//如果每个cell都存在,成功
tableXpath
.
pop
();
//向上一级找
tableXpath
.
pop
();
//向上一级找
if
(
tableXpath
.
length
==
0
)
return
null
}
}
if
(
tableXpath
.
length
<
1
)
return
_isVerified
;
//如果没有共同父节点,返回false
if
(
tableXpath
.
length
<
1
)
return
_isVerified
;
//如果没有共同父节点,返回false
this
.
cells
.
forEach
(
cell
=>
cell
.
confirm
());
//对所有单元格进行确认
this
.
cells
.
forEach
(
cell
=>
cell
.
confirm
());
//对所有单元格进行确认
_isVerified
=
true
;
_isVerified
=
true
;
this
.
xpath
=
tableXpath
.
join
(
"/"
);
this
.
xpath
=
tableXpath
.
join
(
"/"
);
//是否可以转换成SmallXPath
return
_isVerified
;
return
_isVerified
;
}
}
/**
/**
* 返回当前表格的数据
* 返回当前表格的数据
*/
*/
this
.
getData
=
function
()
{
this
.
getData
=
function
()
{
let
rtns
=
[];
//定义返回值
if
(
!
_isVerified
)
return
null
;
//如果还没通过验证不返回数据
if
(
!
_isVerified
)
return
null
;
//如果还没通过验证不返回数据
console
.
log
(
this
.
cells
)
console
.
log
(
this
.
cells
)
let
relativeXpaths
=
[]
let
rowIndex
=
0
;
for
(
let
i
=
0
;
i
<
this
.
cells
.
length
;
i
++
)
{
while
(
true
)
{
relativeXpaths
.
push
(
this
.
cells
[
i
].
relativePath
)
let
result
=
{}
this
.
cells
.
forEach
(
cell
=>
cell
.
getData
(
result
,
rowIndex
));
if
(
result
===
{})
break
;
//不断的根据rowIndex取值,直到取不到值就退出,需要注意如果第0行是表头,也不一定可以取到值
rtns
.
push
(
result
);
//把值推入返回值中
rowIndex
++
;
}
}
// 找出的全部xpath信息
return
rtns
;
let
resultElement
=
combineXPaths
(
this
.
cells
[
0
].
_tableXpathArray
,
relativeXpaths
);
let
xpaths
=
[]
// 当前表的全部xpath
console
.
log
(
resultElement
)
for
(
let
i
=
0
;
i
<
resultElement
.
length
;
i
++
)
{
for
(
let
k
=
0
;
k
<
resultElement
[
i
].
paths
.
length
;
k
++
)
{
xpaths
.
push
(
resultElement
[
i
].
paths
[
k
])
}
}
console
.
log
(
xpaths
)
}
}
}
}
/**
/**
...
@@ -95,7 +90,7 @@ function cell (xpath, name = "", type = "", title = "") {
...
@@ -95,7 +90,7 @@ function cell (xpath, name = "", type = "", title = "") {
/** 单元格在表上的表头信息*/
/** 单元格在表上的表头信息*/
this
.
title
=
title
;
this
.
title
=
title
;
/**当表结构验证成功后,正确的表绝对路径 */
/**当表结构验证成功后,正确的表绝对路径
,不包含下标
*/
this
.
tableXpath
=
null
;
this
.
tableXpath
=
null
;
/**当表结构验证成功后,正确的单元格相对路径 */
/**当表结构验证成功后,正确的单元格相对路径 */
this
.
relativeXpath
=
null
;
this
.
relativeXpath
=
null
;
...
@@ -136,6 +131,8 @@ function cell (xpath, name = "", type = "", title = "") {
...
@@ -136,6 +131,8 @@ function cell (xpath, name = "", type = "", title = "") {
_tableXpathArray
=
_tableXpathArray
.
join
(
'/'
)
_tableXpathArray
=
_tableXpathArray
.
join
(
'/'
)
_relativeXpathArray
=
_relativeXpathArray
.
join
(
'/'
)
_relativeXpathArray
=
_relativeXpathArray
.
join
(
'/'
)
//table[0]+relativeXpath,可以不存在,因为可能是表头,1,2
//table[0]+relativeXpath,可以不存在,因为可能是表头,1,2
//let result = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
//return !!result.singleNodeValue;
for
(
let
i
=
0
;
i
<
3
;
i
++
)
{
for
(
let
i
=
0
;
i
<
3
;
i
++
)
{
let
isGetElement
=
elementExists
(
_tableXpathArray
+
'['
+
(
i
+
1
)
+
']'
+
'/'
+
_relativeXpathArray
)
let
isGetElement
=
elementExists
(
_tableXpathArray
+
'['
+
(
i
+
1
)
+
']'
+
'/'
+
_relativeXpathArray
)
if
(
isGetElement
)
{
if
(
isGetElement
)
{
...
@@ -149,306 +146,19 @@ function cell (xpath, name = "", type = "", title = "") {
...
@@ -149,306 +146,19 @@ function cell (xpath, name = "", type = "", title = "") {
}
}
/**当所有单元格验证成功后,由表发起确认动作 */
/**当所有单元格验证成功后,由表发起确认动作 */
this
.
confirm
=
function
()
{
this
.
confirm
=
function
()
{
this
.
tableXpath
=
this
.
_tableXpathArray
.
join
(
"/"
);
this
.
tableXpath
=
this
.
_tableXpathArray
.
join
(
"/"
);
//处理下标的事
this
.
relativePath
=
this
.
_relativeXpathArray
.
join
(
"/"
);
this
.
relativePath
=
this
.
_relativeXpathArray
.
join
(
"/"
);
}
}
/**
/**
* 根据索引获取值
* 根据索引获取值,返回{name:value}格式
* @param {any} index 索引号
* @param {object} result 需要赋值的对象
* @param {int} index 索引号
*/
*/
this
.
getValue
=
function
(
index
)
{
this
.
getData
=
function
(
result
,
index
)
{
let
realXpath
=
tableXpath
+
"["
+
index
+
"]"
+
this
.
relativeXpath
;
document
.
getElementById
();
this
.
type
result
[
this
.
name
]
=
"根据type获取到的值"
;
}
}
return
this
;
//返回当前对象
return
this
;
//返回当前对象
}
}
/**
* 检测传入的XPath是否有表的存在,如果没有返回null
* @param {cell[]} tags 元素集合{xpath=XPath,name=返回的属性名,title=标题信息,type=操作类型:文本,链接,元素路径(点击动作用),relativePath=相对路径(最终的)}
* @returns {any} 返回对象信息{}
*/
function
checkTableFromXpaths
(
cells
)
{
let
rtns
=
null
;
let
absolutePaths
=
[];
//绝对路径集合
let
relativeXpaths
=
[];
//相对路径集合
let
tableData
=
[]
//最后界面展示数据
//先找到共同的根以及各路径在共同根下的相对路径
let
parent
=
getCommonParentXPath
(
cells
);
if
(
parent
==
null
||
parent
.
length
==
0
)
return
rtns
;
//如果找不到直接返回null
cells
.
forEach
(
o
=>
absolutePaths
.
push
(
o
.
updateRelativePath
(
parent
)));
//更新每个cell的相对路径
while
(
parent
.
length
>
0
)
{
if
(
verifyTable
(
parent
,
absolutePaths
,
cells
))
break
;
//如果验证表结构成功,就可以退出了
// parent.pop();//向上一级找
// cells.forEach(o => o.updateRelativePath(parent));//更新每个cell的相对路径
}
if
(
parent
.
length
===
0
)
return
rtns
;
//如果父对象都为空了,直接返回null
// 循环数组将相对路径取出
for
(
let
i
=
0
;
i
<
cells
.
length
;
i
++
)
{
let
relativeXpath
=
cells
[
i
].
RelativeXpath
.
join
(
'/'
)
// 转换字符
relativeXpaths
.
push
(
relativeXpath
)
}
// let tableData = getTableData(cells[0].ParentXpath, relativeXpaths);//根据验证后的信息来获取表
// console.log(tableData)
// 获取当前表结构的所有xpath,
let
resultElement
=
combineXPaths
(
cells
[
0
].
ParentXpath
,
relativeXpaths
);
// 表数据查找 返回界面需要的表结构
tableData
=
getTableData
(
resultElement
)
// 返回最终结果
rtns
=
{
parent
:
cells
[
0
].
ParentXpath
,
relativeXpaths
:
relativeXpaths
,
tableData
:
tableData
};
return
rtns
}
/**
* 返回多个元素XPath的共同父节点,返回数组以方便向上移动
* @param {string[]} absolutePaths Xpath绝对路径集合
* @returns {string[]} 返回string[]的共同的根的各部分的集合
*/
function
getCommonParentXPath
(
tags
)
{
let
rtns
=
null
;
// 检查输入参数是否有效
if
(
!
tags
||
tags
.
length
===
0
)
return
null
;
let
allXpaths
=
tags
.
map
(
path
=>
path
.
xpath
.
split
(
'/'
));
// 分割每个路径为组成部分
rtns
=
[];
// 查找公共前缀
for
(
let
i
=
0
;
i
<
allXpaths
[
0
].
length
;
i
++
)
{
if
(
allXpaths
.
every
(
path
=>
path
[
i
]
===
allXpaths
[
0
][
i
]))
{
// 检查所有路径是否有这个部分
rtns
.
push
(
allXpaths
[
0
][
i
]);
}
else
{
break
;
}
//如果发现不同的,直接就可以退出了
}
return
rtns
;
}
/**
* 根据共同的父节点,获取各绝对路径的相对路径,这个方法分离开以便复用
* @param {string[]} parentXpath 父节点路径集合
* @param {string[]} absoluteXpaths 绝对路径集合
* @returns {string[]} 返回string[]的相对路径集合
*/
function
getRelativeXPaths
(
parentXpath
,
absoluteXpaths
)
{
let
rtns
=
[];
let
splitPaths
=
absolutePaths
.
map
(
path
=>
path
.
split
(
'/'
));
splitPaths
.
map
(
path
=>
rtns
.
push
(
path
.
slice
(
parentXpath
.
length
)));
return
rtns
;
}
/**
* 验证表结构是否正确
* @param {string[]} parentXPath 父节点路径集合
* @param {string[]} relativeXPaths 相对路径集合
* @param {cells[]} cells 元素集合{xpath=XPath,name=返回的属性名,title=标题信息,type=操作类型:文本,链接,元素路径(点击动作用),relativePath=相对路径(最终的)}
* @returns {boolean} 返回true为表结构正确
*/
function
verifyTable
(
parentXPath
,
relativeXPaths
,
cells
)
{
// 验证初始路径是否存在
if
(
!
allElementsExist
(
relativeXPaths
))
{
return
false
;
}
parentXPath
=
parentXPath
.
join
(
'/'
)
// 去除相同部分并保留不同的部分
let
modifiedRelativeXPaths
=
relativeXPaths
.
map
(
path
=>
removeCommonPart
(
path
,
parentXPath
));
// 自增 parentXPath 的最后一个节点的下标
parentXPath
=
incrementLastIndex
(
parentXPath
);
// 拼接新路径并验证
let
newPaths
=
modifiedRelativeXPaths
.
map
(
relativePath
=>
parentXPath
+
relativePath
);
// console.log(newPaths)
return
allElementsExist
(
newPaths
,
cells
);
}
/**
* 验证路径是否都能被找到
* @param {array[]} paths 拼接的xpath集合,
* @param {cells[]} cells 单元格数组
* @returns {boolean} 返回true,组合的路径都能被找到
*/
function
allElementsExist
(
paths
,
cells
)
{
// 实现一个函数来检查所有路径是否存在
return
paths
.
every
(
path
=>
elementExists
(
path
,
cells
));
}
/**
* 该函数用来移出绝对路径中的相同父节点部分
* @param {srting} paths 绝对路径
* @param {srting} commonPart 父节点
*/
function
removeCommonPart
(
path
,
commonPart
)
{
// 实现一个函数来移除路径中的共同部分
return
path
.
replace
(
commonPart
,
""
);
}
/**
* 处理xpath父节点的最后一个节点下标
*/
function
incrementLastIndex
(
xpath
)
{
// 将 XPath 分割为节点数组
let
parts
=
xpath
.
split
(
'/'
);
// 找到最后一个节点
let
lastIndex
=
parts
.
length
-
1
;
let
lastNode
=
parts
[
lastIndex
];
// 正则表达式用于找到节点中的数字部分
let
regex
=
/
(\d
+
)(?!
.*
\d)
/
;
// 如果最后一个节点包含数字,则增加该数字
if
(
lastNode
.
match
(
regex
))
{
lastNode
=
lastNode
.
replace
(
regex
,
function
(
match
)
{
return
parseInt
(
match
)
+
1
;
});
}
else
{
// 如果没有数字,假设是第一个索引并添加 [1]
lastNode
+=
"[1]"
;
}
// 重新组装 XPath
parts
[
lastIndex
]
=
lastNode
;
return
parts
.
join
(
'/'
);
}
/**
* 检查XPath是否指向一个存在的元素,如果存在就返回,当父节点的最后一个下标换成第二行的下标时,组合同样的相对路径,向上查找相同的父节点并更新cell对应的父节点,保证当前选中是表结构
*/
function
elementExists
(
xpath
)
{
let
result
=
document
.
evaluate
(
xpath
,
document
,
null
,
XPathResult
.
FIRST_ORDERED_NODE_TYPE
,
null
);
return
!!
result
.
singleNodeValue
;
}
/**
* // 公共部分与拆解部分组合查询 找出当前节点下所有符合条件的表数据
* @param {string} commonXPath 父节点路径
* @param {string[]} relativeXPaths 相对路径集合
* @returns {any} 返回表格内所有符合条件的Xpath
*/
function
combineXPaths
(
commonXPath
,
paths
)
{
// console.log(commonXPath, paths)
// 然后去除公共路径最后一个元素的下标
// let samePath =''
let
commonPath
=
[];
commonPath
=
[...
commonXPath
]
if
(
commonPath
.
length
>
0
)
{
let
lastElement
=
commonPath
[
commonPath
.
length
-
1
];
let
lastElementWithoutIndex
=
lastElement
.
split
(
'['
)[
0
];
commonPath
[
commonPath
.
length
-
1
]
=
lastElementWithoutIndex
;
}
commonXPath
=
commonPath
.
join
(
'/'
)
// 更新公共根节点路径,去除最后一个元素的下标
// let updatedCommonPathStr = commonPath.join('/');
let
content
=
[];
let
index
=
0
;
// 从给定的XPath中去除最后一个节点
function
removeLastNode
(
xpath
)
{
let
parts
=
xpath
.
split
(
'/'
);
parts
.
pop
();
return
parts
.
join
(
'/'
);
}
// 检查XPath是否指向一个存在的元素
function
elementExists
(
xpath
)
{
let
result
=
document
.
evaluate
(
xpath
,
document
,
null
,
XPathResult
.
FIRST_ORDERED_NODE_TYPE
,
null
);
return
!!
result
.
singleNodeValue
;
}
while
(
true
)
{
let
indexedCommonXPath
=
commonXPath
+
'['
+
(
index
+
1
)
+
']'
;
// XPath的下标从1开始
// 如果当前下标的元素不存在,则向上查找
while
(
!
elementExists
(
indexedCommonXPath
)
&&
indexedCommonXPath
.
length
>
commonXPath
.
length
)
{
indexedCommonXPath
=
removeLastNode
(
indexedCommonXPath
);
}
// 如果找到公共XPath层,则停止查找
if
(
indexedCommonXPath
.
length
<=
commonXPath
.
length
)
{
break
;
}
// 将找到的XPath与路径组合,并存储在content数组中
let
combinedPaths
=
paths
.
map
(
path
=>
indexedCommonXPath
+
'/'
+
path
);
content
.
push
({
index
:
index
,
paths
:
combinedPaths
});
index
++
;
}
return
content
;
}
/**
* // 根据找出的表结构的所有cell的Xpath 进行元素内容查找
* @param {string[]} arrayObjects 相对路径集合
* @returns {any} 返回已找到的数据
*/
function
findElements
(
arrayObjects
)
{
let
results
=
[];
// 从给定的XPath中去除最后一个节点
function
removeLastNode
(
xpath
)
{
let
parts
=
xpath
.
split
(
'/'
);
parts
.
pop
();
return
parts
.
join
(
'/'
);
}
// 检查XPath是否指向一个存在的元素
function
elementExistss
(
xpath
)
{
let
result
=
document
.
evaluate
(
xpath
,
document
,
null
,
XPathResult
.
FIRST_ORDERED_NODE_TYPE
,
null
);
return
!!
result
.
singleNodeValue
;
}
// 对每个paths数组进行查找
arrayObjects
.
forEach
(
item
=>
{
let
groupResult
=
{
index
:
item
.
index
,
elements
:
[]
};
item
.
paths
.
forEach
(
path
=>
{
let
currentXPath
=
path
;
// 从XPath的最后一个节点开始查找
// console.log(!elementExistss(currentXPath))
while
(
!
elementExistss
(
currentXPath
)
&&
currentXPath
!==
''
)
{
currentXPath
=
removeLastNode
(
currentXPath
);
}
// 如果找到了元素
if
(
currentXPath
!==
''
)
{
let
result
=
document
.
evaluate
(
currentXPath
,
document
,
null
,
XPathResult
.
FIRST_ORDERED_NODE_TYPE
,
null
);
let
element
=
result
.
singleNodeValue
;
groupResult
.
elements
.
push
({
xpath
:
currentXPath
,
content
:
element
.
textContent
});
}
});
results
.
push
(
groupResult
);
});
return
results
;
}
/**
* // 表结构处理,处理成界面需要的结构
* @param {string[]} resultElement 已经找到的所有数据
* @returns {any} 返回已找到的数据
*/
function
getTableData
(
resultElement
)
{
let
foundElements
=
[]
let
transformedData
=
[]
// 判断是否有数据
if
(
resultElement
)
{
// console.log("Element found: ", resultElement);
// 将页面过滤出来的元素的xpath进行元素查找,并且返回元素的xpath和内容
foundElements
=
findElements
(
resultElement
);
// console.log(foundElements);
// 返回表格需要的格式
transformedData
=
foundElements
.
map
((
group
,
index
)
=>
{
let
transformedGroup
=
{};
group
.
elements
.
forEach
((
element
,
elemIndex
)
=>
{
transformedGroup
[
`content
${
elemIndex
}
`
]
=
{
Xpath
:
element
.
xpath
,
content
:
element
.
content
,
otherName
:
"请填写标题"
};
});
transformedGroup
[
"_X_ROW_KEY"
]
=
`row_
${
index
+
0
}
`
;
// 假设起始索引为8
return
transformedGroup
;
});
return
transformedData
;
}
else
{
return
null
}
}
export
{
cell
,
checkTableFromXpaths
,
table
}
export
{
cell
,
checkTableFromXpaths
,
table
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment