public IEnumerable<TestCaseData> DataProvider() { using (var driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver()) { driver.Navigate().GoToUrl("https://www.google.com/search?q=test"); // this.BlockUntilElementIsAvailable(driver, "input"); //Thread.Sleep(500); // needed for PhantomJS. Chrome works without. // load JS onto a page driver.ExecuteScript(File.ReadAllText("scripts/jasmine.js")); driver.ExecuteScript(File.ReadAllText("scripts/jquery-2.1.4.js")); driver.ExecuteScript(File.ReadAllText("scripts/boot.js")); // this contains both reporter and bootstrapper driver.ExecuteScript(File.ReadAllText("scripts/googleTests.js")); driver.ExecuteScript(File.ReadAllText("scripts/execute.js")); // wait until all tests are done while ((driver.ExecuteScript("return ReportCollector.Status;") as string) != "Finished") { Thread.Sleep(100); } // now get the results var specResultsJson = (string)driver.ExecuteScript("return JSON.stringify(ReportCollector.SpecResults);"); var results = JsonConvert.DeserializeObject<JasmineResults[]>(specResultsJson); // and create test case data foreach (var result in results) { string msg = null; if (result.status == "passed") { msg = result.status; } else { foreach (var item in result.failedExpectations) { msg += "Matcher: " + item.matcherName + "; Message: " + item.message + Environment.NewLine; } } var testCase = new TestCaseData(msg) .SetDescription(result.description) .SetName(result.fullName) .Returns("passed"); yield return testCase; } } }
public void TestMethod1() { var browser = new OpenQA.Selenium.PhantomJS.PhantomJSDriver(); var dir = Directory.CreateDirectory(@"c:\temp\news"); for (int i = 0; i < 1000; i++) { browser.Navigate().GoToUrl("http://webadvice.no/svada"); var svada = browser.FindElementById("content").FindElement(By.ClassName("wrap")); using (var reader = new StringReader(svada.Text)) { reader.ReadLine(); // header //reader.ReadLine(); var title = reader.ReadLine(); var ingress = reader.ReadLine(); var content = new StringBuilder(); var iterations = 0; while (reader.Peek() != -1) { string header = ""; reader.ReadLine(); // br if (iterations++ < 5) { header = "<h3>" + reader.ReadLine() + "</h3>"; } var paragraph = reader.ReadLine(); content.AppendFormat("{0}<p>{1}</p>", header, paragraph); } Console.Out.WriteLine("Title: " + title); Console.Out.WriteLine("Ingress: " + ingress); Console.Out.WriteLine("Body: " + content); using (var writer = new StreamWriter(@"c:\temp\news\news" + i + ".txt")) { writer.WriteLine(title); writer.WriteLine(ingress); writer.WriteLine(content); } } } browser.Close(); }
private static List <EmailInfo> GetEmails(OpenQA.Selenium.PhantomJS.PhantomJSDriver driver) { GoToEmailOverview(driver); var list = new List <EmailInfo>(); // email accounts var elements = driver.FindElementsByCssSelector(".accounts tbody tr td:first-child span"); foreach (var element in elements) { var email = element.Text.Trim(); var name = email.Substring(0, email.IndexOf("@")); var editLink = "https://www.one.com/admin/edit-account.do?name=" + name; var info = new EmailInfo(); info.Email = email; info.EditUrl = editLink; list.Add(info); } // aliases (we should probably use this one) elements = driver.FindElementsByCssSelector(".aliases .labeled td:first-child span"); foreach (var element in elements) { var email = element.Text.Trim(); var name = email.Substring(0, email.IndexOf("@")); var editLink = "https://www.one.com/admin/edit-alias.do?name=" + name; var info = new EmailInfo(); info.Email = email; info.EditUrl = editLink; list.Add(info); } // add forward addresses foreach (var info in list) { driver.Navigate().GoToUrl(info.EditUrl); var forwardElements = driver.FindElementsByCssSelector(".forwards tr td:first-child span,#forward tbody tr td:first-child span"); foreach (var item in forwardElements) { info.ForwardAddresses.Add(item.Text.Trim()); } } return(list); }
private static void LoginUser(OpenQA.Selenium.PhantomJS.PhantomJSDriver driver, LoginInfo login) { driver.Navigate().GoToUrl("https://login.one.com/cp/"); var username = driver.FindElement(By.Name("displayUsername")); username.SendKeys(login.UserName); var password = driver.FindElement(By.ClassName("password")); password.SendKeys(login.Password); var button = driver.FindElement(By.ClassName("oneButton")); button.Click(); }
private static void RemoveForwardEmail(OpenQA.Selenium.PhantomJS.PhantomJSDriver driver, EmailInfo emailInfo, string forwardEmail) { driver.Navigate().GoToUrl(emailInfo.EditUrl); var rows = driver.FindElementsByCssSelector("#forward tbody tr,.forwards tbody tr"); foreach (var row in rows) { var rowEmail = row.FindElement(By.CssSelector("td:first-child span")).Text; var rowRemoveLink = row.FindElement(By.CssSelector("a")); if (rowEmail == forwardEmail) { rowRemoveLink.Click(); var submitBtn = driver.FindElementByCssSelector(".buttons input[type=submit].arrow-right"); submitBtn.Click(); return; } } }
private static void AddForwardEmail(OpenQA.Selenium.PhantomJS.PhantomJSDriver driver, EmailInfo emailInfo, string forwardEmail) { var hasForwardEmail = emailInfo.ForwardAddresses.FirstOrDefault(f => f == forwardEmail) != null; if (hasForwardEmail) { return; } driver.Navigate().GoToUrl(emailInfo.EditUrl); var recipientElement = driver.FindElementById("addRecipient"); recipientElement.SendKeys(forwardEmail); var AddBtn = driver.FindElementByCssSelector(".addForwardsRecipient input[type=submit]"); AddBtn.Click(); var submitBtn = driver.FindElementByCssSelector(".buttons input[type=submit].arrow-right"); submitBtn.Click(); }
protected override List <Packet> FetchPackets(Account account) { var tick = DateTime.Now.Ticks; // open page try { driver = Seeker.Service.GetDriver(); Teller.Log($"[{account.Name}] 페이지를 초기화..."); driver.Manage().Window.Size = new Size(765, 1000); driver.Navigate().GoToUrl("https://obank1.kbstar.com/quics?page=C025255&cc=b028364:b028702");// fill the form Teller.Log($"[{account.Name}] 로딩이 끝나기를 기다림..."); var timeoutSeconds = account.IntervalSeconds; if (timeoutSeconds < 30) { timeoutSeconds = 30; } var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutSeconds)); var loading = By.Id("loading"); wait.Until(ExpectedConditions.InvisibilityOfElementLocated(loading)); Teller.Log($"[{account.Name}] 계좌 정보 입력중..."); var account_num = By.Id("account_num"); wait.Until(ExpectedConditions.ElementToBeClickable(account_num)); driver.FindElement(account_num).SendKeys(account.Number.Replace("-", "")); var user_id = By.Id("user_id"); wait.Until(ExpectedConditions.ElementToBeClickable(user_id)); driver.FindElement(user_id).SendKeys(account.UserId); new SelectElement(driver.FindElementById("조회시작년")).SelectByValue(account.From.ToString("yyyy")); new SelectElement(driver.FindElementById("조회시작월")).SelectByValue(account.From.ToString("MM")); new SelectElement(driver.FindElementById("조회시작일")).SelectByValue(account.From.ToString("dd")); new SelectElement(driver.FindElementById("조회끝년")).SelectByValue(account.To.ToString("yyyy")); new SelectElement(driver.FindElementById("조회끝월")).SelectByValue(account.To.ToString("MM")); new SelectElement(driver.FindElementById("조회끝일")).SelectByValue(account.To.ToString("dd")); // open password keypad Teller.Log($"[{account.Name}] 비밀번호 키패드 이미지 추출..."); var password = By.Id("비밀번호"); wait.Until(ExpectedConditions.ElementToBeClickable(password)); driver.FindElement(password).Click(); // save keypad image var keypadImagePath = ContentManager.getPath($@"KB/{tick}.keypad.bmp"); var keypad = By.CssSelector(".keypadWrap img"); wait.Until(ExpectedConditions.ElementToBeClickable(keypad)); driver.GetScreenshot().SaveAsFile(keypadImagePath, ScreenshotImageFormat.Bmp); using (var bitmapKeypad = Bitmap.FromFile(keypadImagePath)) { // analyze keypad image var element = driver.FindElement(keypad); var area = new { left = element.Location.X, center = element.Location.X + element.Size.Width / 2, right = element.Location.X + element.Size.Width, width = element.Size.Width, top = element.Location.Y, height = element.Size.Height, }; var btnLocations = new Dictionary <string, Point>() { { "num1", new Point(area.left + 46, area.top + 70) }, { "num2", new Point(area.center, area.top + 70) }, { "num3", new Point(area.right - 46, area.top + 70) }, { "num4", new Point(area.left + 46, area.top + 130) }, { "xxx1", new Point(area.center, area.top + 130) }, { "num6", new Point(area.right - 46, area.top + 130) }, { "xxx2", new Point(area.left + 46, area.top + 188) }, { "xxx3", new Point(area.center, area.top + 188) }, { "xxx4", new Point(area.right - 46, area.top + 188) }, { "delOne", new Point(area.left + 46, area.top + 246) }, { "xxx5", new Point(area.center, area.top + 246) }, { "delAll", new Point(area.right - 46, area.top + 246) }, { "submit", new Point(area.center, area.top + 300) }, }; // SOLUTION IMAGES' digests var SIGMA = 1; var GAMMA = 1.5; var DEGREE = 180; var solDigests = new Dictionary <string, Digest>() { { "num5", ImagePhash.ComputeDigest(ContentManager.getPath(@"KB/KB_keypad_sol_5.bmp"), SIGMA, GAMMA, DEGREE) }, { "num7", ImagePhash.ComputeDigest(ContentManager.getPath(@"KB/KB_keypad_sol_7.bmp"), SIGMA, GAMMA, DEGREE) }, { "num8", ImagePhash.ComputeDigest(ContentManager.getPath(@"KB/KB_keypad_sol_8.bmp"), SIGMA, GAMMA, DEGREE) }, { "num9", ImagePhash.ComputeDigest(ContentManager.getPath(@"KB/KB_keypad_sol_9.bmp"), SIGMA, GAMMA, DEGREE) }, { "num0", ImagePhash.ComputeDigest(ContentManager.getPath(@"KB/KB_keypad_sol_0.bmp"), SIGMA, GAMMA, DEGREE) }, }; // find each digit of btns by correlation with solution image Teller.Log($"[{account.Name}] 번호별로 이미지 추출, 해싱을 통해 상관관계 분석..."); var solBtnLocations = new Dictionary <string, Point>(); foreach (var btnLocation in btnLocations) { // compare only xxx btns (order shuffled btns 5,7,8,9,0) if (!btnLocation.Key.StartsWith("xxx")) { solBtnLocations.Add(btnLocation.Key, btnLocation.Value); continue; } // crop image var btnImagePath = ContentManager.getPath($@"KB/{tick}.{btnLocation.Key}.bmp"); var rect = new Rectangle(btnLocation.Value.X - 22, btnLocation.Value.Y - 22, 44, 44); using (var bitmap = new Bitmap(rect.Width, rect.Height)) using (var graphic = Graphics.FromImage(bitmap)) { graphic.DrawImage(bitmapKeypad, 0, 0, rect, GraphicsUnit.Pixel); bitmap.Save(btnImagePath); // find each digit for btns var btnDigest = ImagePhash.ComputeDigest(btnImagePath, SIGMA, GAMMA, DEGREE); string answerKey = null; double answerCorr = 0; foreach (var solDigest in solDigests) { var corr = ImagePhash.GetCrossCorrelation(btnDigest, solDigest.Value); if (corr > answerCorr) { answerCorr = corr; answerKey = solDigest.Key; } } // add to solved btn locations try { solBtnLocations.Add(answerKey, btnLocation.Value); Teller.Log($"[{account.Name}] {answerKey} 분석 완료..."); } catch (ArgumentException) { Teller.Log($"[{account.Name}] {btnLocation.Key}를 확정 할 수 없음... 재시작"); throw new NeedToRefetchError(); } } } // now tocuh the keypad as the solution Point locationTo; var zero = driver.FindElement(password); var dx = -area.left + 90; var dy = -area.top + 15; // // helper to adjust dx,dy // driver.ExecuteScript(@" //window.onclick = function(e) { // var d = document.createElement('div'); // d.style.width='2px'; d.style.height='2px'; // d.style.position='absolute'; // d.style.display='block'; // d.style.top=e.clientY+'px'; // d.style.left=e.clientX+'px'; // d.style.background='red'; // d.style.zIndex='10000000'; // document.body.appendChild(d); //}; // "); foreach (var digit in account.Password.ToCharArray()) { locationTo = solBtnLocations["num" + digit]; new Actions(driver).MoveToElement(zero).MoveByOffset(locationTo.X + dx, locationTo.Y + dy).Click().Perform(); Teller.Log($"[{account.Name}] 번호 ({locationTo.X + dx}, {locationTo.Y + dy}) 클릭..."); } locationTo = btnLocations["submit"]; // (373, 554). new Actions(driver).MoveToElement(zero).MoveByOffset(locationTo.X + dx, locationTo.Y + dy).Click().Perform(); Teller.Log($"[{account.Name}] 확인 ({locationTo.X + dx}, {locationTo.Y + dy}) 클릭..."); // submit the form wait.Until(ExpectedConditions.InvisibilityOfElementLocated(keypad)); var submit = By.CssSelector("input[type=submit]"); driver.FindElement(submit).Click(); } // check page loaded var table = By.CssSelector(".tType01"); wait.Until(ExpectedConditions.ElementIsVisible(table)); // reprocessing as Packet def List <Packet> packets = new List <Packet>(); for (var pageNum = 0; true; pageNum++) { Teller.Log($"[{account.Name}] {pageNum + 1} 페이지 파싱 및 분석..."); //driver.GetScreenshot().SaveAsFile(ContentManager.getPath($@"KB/KB_result_{pageNum}.bmp"), ScreenshotImageFormat.Bmp); var trs = driver.FindElementsByCssSelector(".tType01 tbody tr"); if (trs.Count < 2) { Teller.Log($"[{account.Name}] {pageNum + 1} 페이지 거래 내역 없음. 페이지 종료..."); break; // No items } for (var i = 0; i < trs.Count; i++) { var tr = trs[i]; if (i % 2 == 0) { var datetimeTemp = tr.FindElement(By.CssSelector("td:nth-child(1)")).GetAttribute("textContent").Trim(); datetimeTemp = datetimeTemp.Substring(0, 10) + " " + datetimeTemp.Substring(10); var packet = new Packet { Date = Convert.ToDateTime(datetimeTemp), Note = tr.FindElement(By.CssSelector("td:nth-child(2)")).GetAttribute("textContent").Trim(), OutName = tr.FindElement(By.CssSelector("td:nth-child(3)")).GetAttribute("textContent").Trim(), OutAmount = Convert.ToDecimal(tr.FindElement(By.CssSelector("td:nth-child(4)")).GetAttribute("textContent").Trim()), InAmount = Convert.ToDecimal(tr.FindElement(By.CssSelector("td:nth-child(5)")).GetAttribute("textContent").Trim()), Balance = Convert.ToDecimal(tr.FindElement(By.CssSelector("td:nth-child(6)")).GetAttribute("textContent").Trim()), Bank = tr.FindElement(By.CssSelector("td:nth-child(7)")).GetAttribute("textContent").Trim(), Type = tr.FindElement(By.CssSelector("td:nth-child(8)")).GetAttribute("textContent").Trim(), }; packets.Add(packet); } else { packets[(i - 1) / 2].InName = tr.GetAttribute("textContent").Trim(); } } // for pagination try { driver.FindElementByCssSelector(".optionBtnArea .leftArea .next input").Click(); wait.Until(ExpectedConditions.ElementIsVisible(By.Id("loading"))); wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.Id("loading"))); } catch (NoSuchElementException) { Teller.Log($"[{account.Name}] 페이지 종료..."); break; } } return(packets); } catch (WebDriverException e) { Teller.Log($"[{account.Name}] 가상 브라우저 비정상 종료...\n{e.Message}"); throw new NeedToRefetchError(); } catch (NeedToRefetchError) { throw; } catch (Exception e) { Teller.Log($"[{account.Name}] 처리되지 않은 예외 발생...\n{e.Message}"); throw new NeedToRefetchError(); } finally { // delete temp files DisposeFile($@"KB/{tick}.keypad.bmp"); DisposeFile($@"KB/{tick}.xxx1.bmp"); DisposeFile($@"KB/{tick}.xxx2.bmp"); DisposeFile($@"KB/{tick}.xxx3.bmp"); DisposeFile($@"KB/{tick}.xxx4.bmp"); DisposeFile($@"KB/{tick}.xxx5.bmp"); Dispose(); } }
public Location GetLocation(string urlRe, string province, string district, string pacelNo) { var result = new Location(); var parcel = 0; if (string.IsNullOrEmpty(province) || string.IsNullOrEmpty(district) || string.IsNullOrEmpty(pacelNo)) { return result; } if (pacelNo.Contains("-")) { var stat = int.TryParse(pacelNo.Split('-')[0], out parcel); if (!stat) return result; } else { var stat = int.TryParse(pacelNo, out parcel); if (!stat) return result; } var loc = DataHelper.GetLocation(province, district, parcel); if (loc != null) { return loc; } string driverPath = @"D:\MyProjects\FRES\src\FRES.Source.Map\Drivers\"; IWebDriver driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver(driverPath); //IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(driverPath); try { var url = "http://dolwms.dol.go.th/tvwebp/"; driver.Navigate().GoToUrl(url); var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(TIMEOUT)); Thread.Sleep(DELAY); var ddlProvince = driver.FindSelectElementWhenPopulated(By.Name("ddlProvince"), TIMEOUT); ddlProvince.SelectBySubText(province); Thread.Sleep(DELAY); var ddlAmphur = driver.FindSelectElementWhenPopulated(By.Name("ddlAmphur"), TIMEOUT); ddlAmphur.SelectBySubText(district); Thread.Sleep(DELAY); var txtPacelNo = new WebDriverWait(driver, TimeSpan.FromSeconds(TIMEOUT)).Until(ExpectedConditions.ElementExists(By.Name("txtPacelNo"))); txtPacelNo.SendKeys(pacelNo); Thread.Sleep(DELAY); var btnFind = driver.FindElement(By.Name("btnFind")); IJavaScriptExecutor js = driver as IJavaScriptExecutor; js.ExecuteScript("arguments[0].click();", btnFind); Thread.Sleep(DELAY); //var element = new WebDriverWait(driver, TimeSpan.FromSeconds(3)).Until(ExpectedConditions.TextToBePresentInElement(driver.FindElement(By.Id("ddlAmphur")), "01")); //wait.Until(ExpectedConditions.ElementExists(By.CssSelector("div[style=\"transform: translateZ(0px); position: absolute; left: 0px; top: 0px; z-index: 107; width: 100%;\"]"))); var isExist = wait.Until((d) => { return driver.PageSource.Contains("createMarker( new Array("); }); if (isExist) { var html = driver.PageSource; html = GetStrBtw(html, "createMarker( new Array(", "));");//.Replace("'", string.Empty); var dtls = html.Split(',').Select(x => x.Replace("'", "")).ToArray(); result = new Location { Amphur = district, Province = province, ParcelCode = parcel, Lat = double.Parse(dtls[7]), Lon = double.Parse(dtls[8]) }; DataHelper.InsertLocation(result); } else { throw new Exception("Can't find location"); } } catch (Exception ex) { lock (sync) File.AppendAllText("D:/RE/M.log", DateTime.Now.ToString("yyyyMMdd HH:mm") + "," + province + "," + district + "," + parcel + "," + urlRe + "," + ex.GetBaseException().Message + "\r\n"); } finally { //Thread.Sleep(3000); driver.Close(); driver.Quit(); driver.Dispose(); } return result; }