엔터프라이즈 애플리케이션을 위해 iOS 장치 및 시뮬레이터에서 좀 더 고급 자동화를 수행하고 있습니다. 자동화는 브라우저없는 자바 스크립트로 작성됩니다. 방법 중 하나는 시뮬레이터가 아닌 장치에서 작동하므로 해결 방법을 코딩해야합니다. 궁금하다면 UIATarget.localTarget (). frontMostApp (). preferencesValueForKey (key)입니다.
우리가해야 할 일은 디스크의 plist 파일에서 서버 경로 (다양 함)를 읽는 것입니다. 시뮬레이터의 해결 방법으로 다음 줄을 사용하여 환경 설정이 포함 된 plist 파일을 찾았습니다.
// Get the alias of the user who's logged in
var result = UIATarget.localTarget().host().performTaskWithPathArgumentsTimeout("/usr/bin/whoami", [], 5).stdout;
// Remove the extra newline at the end of the alias we got
result = result.replace('\n',"");
// Find the location of the plist containing the server info
result = UIATarget.localTarget().host().performTaskWithPathArgumentsTimeout("/usr/bin/find", ["/Users/"+result+"/Library/Application Support/iPhone Simulator", "-name", "redacted.plist"], 100);
// For some reason we need a delay here
UIATarget.localTarget().delay(.5);
// Results are returned in a single string separated by newline characters, so we can split it into an array
// This array contains all of the folders which have the plist file under the Simulator directory
var plistLocations = result.stdout.split("\n");
...
// For this example, let's just assume we want slot 0 here to save time
var plistBinaryLocation = plistLocations[0];
var plistXMLLocation = plistLocations[i] + ".xml";
result = UIATarget.localTarget().host().performTaskWithPathArgumentsTimeout("/usr/bin/plutil", ["-convert","xml1", plistBinaryLocation,"-o", plistXMLLocation], 100);
여기에서 내용을 얻는 가장 좋은 방법은 파일을 cat 또는 grep하는 것입니다. 디스크에서 직접 파일을 읽을 수 없기 때문입니다. 그러나 구문을 내리는 데 문제가 있습니다. 내가 읽고있는 plist 파일의 편집 된 스 니펫은 다음과 같습니다.
<key>server_url</key>
<string>http://pathToServer</string>
파일에는 server_url 키가 고유 한 키 / 문자열 쌍이 많이 있습니다. 이상적으로는 룩 백과 같은 일을하고 싶지만 JavaScript가이를 지원하지 않는 것 같기 때문에 파일에서 쌍을 가져 와서 조금 후에 깎아 내리겠다고 생각했습니다.
다음과 같이 키를 검색 할 수 있습니다.
// This line works
var expression = new RegExp(escapeRegExp("<key>server_url</key>"));
if(result.stdout.match(expression))
{
UIALogger.logMessage("FOUND IT!!!");
}
else
{
UIALogger.logMessage("NOPE :(");
}
escapeRegExp 메소드는 다음과 같습니다.
function escapeRegExp(str)
{
var result = str.replace(/([()[{*+.$^\\|?])/g, '\\$1');
UIALogger.logMessage("NEW STRING: " + result);
return result;
}
또한이 줄은 값을 반환하지만 잘못된 줄을 가져옵니다.
var expression = new RegExp(escapeRegExp("<string>(.*?)</string>"));
그러나 두 가지를 합치면 (Regex 구문) 터미널에서는 작동하지만 코드에서는 작동하지 않습니다.
var expression = new RegExp(escapeRegExp("<key>server_url</key>[\s]*<string>(.*?)</string>"));
내가 무엇을 놓치고 있습니까? 나는 또한 운없이 grep과 egrep을 시도했습니다.
여기에서 JavaScript 코드에서 정규식을 사용하는 데 영향을 미치는 두 가지 문제가 있습니다.
(.*?)
및 공백 무시 [\s]*
도 이스케이프되고 예상 한 방식으로 평가되지 않습니다. XML 부분을 이스케이프하고이를 이스케이프하지 않고 정규식 부분을 추가해야합니다.[\s]*
은 JavaScript의 일반 문자열 이스케이프 규칙의 희생양이되고 있습니다. "\ s"는 출력에서 "s"로 바뀝니다. 정규식을 생성하기 위해 전달하는 문자열에서 "\ s"로 유지되도록 해당 백 슬래시를 "\ s"로 이스케이프해야합니다.UI 자동화 엔진 자체에서 확인한 작업 스크립트를 작성했습니다. 예상 URL을 추출하여 인쇄해야합니다.
var testString = "" +
"<plistExample>\n" +
" <key>dont-find-me</key>\n" +
" <string>bad value</string>\n" +
" <key>server_url</key>\n" +
" <string>http://server_url</string>\n" +
"</plistExample>";
function escapeRegExp(str)
{
var result = str.replace(/([()[{*+.$^\\|?])/g, '\\$1');
UIALogger.logMessage("NEW STRING: " + result);
return result;
}
var strExp = escapeRegExp("<key>server_url</key>") + "[\\s]*" + escapeRegExp("<string>") + "(.*)" + escapeRegExp("</string>");
UIALogger.logMessage("Expression escaping only the xml parts:" + strExp);
var exp = new RegExp(strExp);
var match = testString.match(exp);
UIALogger.logMessage("Match: " + match[1]);
하지만 정규식에서 이스케이프해야하는 유일한 것은 XML 닫는 태그의 슬래시뿐이라는 점을 지적해야합니다. 즉, escapeRegExp()
함수 가 필요하지 않으며 다음 과 같이 원하는 표현식을 작성할 수 있습니다.
var exp = new RegExp("<key>server_url<\/key>[\\s]*<string>(.*)<\/string>");
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다