/// <summary>
        ///
        /// </summary>
        /// <param name="logoutAndLogin">logout current webDriver and login again to see all owners</param>
        /// <returns></returns>
        public int fnc_getPageCount(bool logoutAndLogin)
        {
            login lg = new login();

            if (logoutAndLogin)
            {
                lg.fnc_logoutWithSelenium(this.v_webBrowser);
            }
            if (login.fnc_isLoginPage(this.v_webBrowser.Url, this.v_url))
            {
                lg.fnc_loginWithSelenium(this.v_webBrowser);
            }

            this.v_webBrowser.Navigate().GoToUrl(this.v_url);
            SharedFunctions.sb_waitForReady(this.v_webBrowser);
            string html          = this.v_webBrowser.PageSource;
            int    lastPageIndex = 0;
            var    table         = Functions.fnc_getElementById(html, this.v_tableId);

            if (table != null)
            {
                var tbody = table.SelectSingleNode("tbody");
                var rows  = tbody.SelectNodes("tr");
                if (rows != null && rows.Count > 0)
                {
                    var lastRow        = rows[rows.Count - 1];
                    var td             = lastRow.SelectSingleNode("td");
                    var hrefCollection = td.SelectNodes("a");
                    if (hrefCollection != null && hrefCollection.Count > 0)
                    {
                        var pageIndexStr = hrefCollection[hrefCollection.Count - 1].InnerText;
                        int temp;
                        if (int.TryParse(pageIndexStr, out temp))
                        {
                            lastPageIndex = temp;
                        }
                    }
                }
            }
            return(lastPageIndex);
        }
Esempio n. 2
0
        private string fnc_downloadAttach(IWebElement elementDownload, string downloadDirPath, string fileDirPath
                                          , string fileNameWithOutExtension)
        {
            elementDownload.Click();
            SharedFunctions.sb_waitForReady(this.prp_webBrowser);
            FileInfo myFile;

            do
            {
                myFile = (new DirectoryInfo(downloadDirPath)).GetFiles("*.*").OrderByDescending(o => o.LastWriteTime).FirstOrDefault();

                if (myFile == null || myFile.LastWriteTime < DateTime.Now.AddMinutes(-5))
                {
                    //there is error
                    throw new Exception("File cannot be downloaded");
                }
                else if (myFile.Extension != ".crdownload" && myFile.Extension != ".tmp")
                {
                    char[] chArrInvalidFileChars = Path.GetInvalidFileNameChars();
                    char[] chArrInvalidPathChars = Path.GetInvalidPathChars();
                    fileNameWithOutExtension = string.Join("_", fileNameWithOutExtension.Split(chArrInvalidFileChars));
                    fileNameWithOutExtension = string.Join("_", fileNameWithOutExtension.Split(chArrInvalidPathChars));
                    fileNameWithOutExtension = fileDirPath + "\\" + fileNameWithOutExtension + myFile.Extension;
                    if (!Directory.Exists(fileDirPath))
                    {
                        Directory.CreateDirectory(fileDirPath);
                    }
                    if (File.Exists(fileNameWithOutExtension))
                    {
                        File.Delete(fileNameWithOutExtension);
                    }

                    myFile.MoveTo(fileNameWithOutExtension);

                    return(fileNameWithOutExtension);
                }
            } while (myFile.Extension == ".crdownload" || myFile.Extension == ".tmp" /*wait till .crdownload or .tmp to change to .pdf*/);
            return(null);
        }
        public static int fnc_getPageCountWithRowCount(IWebDriver webBrowser, string url, string controlIdRowCount, string controlIdToClick, bool logoutAndLogin)
        {
            login lg = new login();

            if (logoutAndLogin)
            {
                lg.fnc_logoutWithSelenium(webBrowser);
            }
            if (login.fnc_isLoginPage(webBrowser.Url, url))
            {
                lg.fnc_loginWithSelenium(webBrowser);
                webBrowser.Navigate().GoToUrl(url);
            }

            if (webBrowser.Url.ToLower() != url.ToLower())
            {
                webBrowser.Navigate().GoToUrl(url);
                SharedFunctions.sb_waitForReady(webBrowser);
            }

            if (!string.IsNullOrEmpty(controlIdToClick))
            {
                sb_click(webBrowser, url, controlIdToClick, false);
            }
            int lastPageIndex = -1;
            var elRowCount    = webBrowser.FindElement(By.Id(controlIdRowCount));

            //int paginationGroup = 0;
            if (elRowCount != null)
            {
                if (int.TryParse(elRowCount.Text, out lastPageIndex))
                {
                    return(lastPageIndex / v_paginationSize + (lastPageIndex % v_paginationSize > 0 ? 1 : 0));
                }
            }
            return(lastPageIndex);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="logoutAndLogin">logout current webDriver and login again to see all owners</param>
        public int fnc_gotoPage(int pageIndex, bool logoutAndLogin, bool waitTillReady = true)
        {
            login lg = new login();

            if (logoutAndLogin)
            {
                lg.fnc_logoutWithSelenium(this.v_webBrowser);
            }
            if (login.fnc_isLoginPage(this.v_webBrowser.Url, this.v_url))
            {
                lg.fnc_loginWithSelenium(this.v_webBrowser);
            }

            if (this.v_pageCount == -1)
            {
                this.v_pageCount = this.fnc_getPageCount(false);
            }
            if (pageIndex > this.v_pageCount)
            {
                return(-1);
            }

            if (pageIndex == 0)
            {
                this.v_webBrowser.Navigate().GoToUrl(this.v_url);
                if (waitTillReady)
                {
                    SharedFunctions.sb_waitForReady(this.v_webBrowser);
                }
                return(pageIndex);
            }
            else
            {
                this.v_webBrowser.Navigate().GoToUrl(this.v_url);
                SharedFunctions.sb_waitForReady(this.v_webBrowser);
                var table = this.v_webBrowser.FindElementById(v_tableId);
                if (table != null)
                {
                    var rows = table.FindElements(By.TagName("tr"));
                    if (rows != null && rows.Count > 0)
                    {
                        var lastRow = rows[rows.Count - 1];
                        var td      = lastRow.FindElement(By.TagName("td"));
                        if (td != null)
                        {
                            var hrefCollection = td.FindElements(By.TagName("a"));
                            if (pageIndex <= hrefCollection.Count)
                            {
                                hrefCollection[pageIndex - 1].Click();
                                if (waitTillReady)
                                {
                                    SharedFunctions.sb_waitForReady(this.v_webBrowser);
                                }
                                return(pageIndex);
                            }
                            else
                            {
                                return(-1);
                            }
                        }
                        else
                        {
                            return(-1);
                        }
                    }
                    else
                    {
                        return(-1);
                    }
                }
                else
                {
                    return(-1);
                }
            }
        }
        private void sb_readExtraDetailAndSaveToDB(int cycleNumber, int pageIndex)
        {
            int    rowIndex = 1;
            string htmlEditPage;

            nextRecord : pageIndex = this.fnc_gotoPage(pageIndex, true);
            if (pageIndex == -1)
            {
                return;
            }

            var table = this.v_webBrowser.FindElementById(v_tableId);

            if (table != null)
            {
                var rows = table.FindElements(By.TagName("tr"));
                if (rows != null && rows.Count > 0 && rowIndex <= rows.Count - 2)
                {//first and last row are header and footer
                    using (var entityLogistic = new Model.logisticEntities())
                    {
                        var columns = rows[rowIndex].FindElements(By.TagName("td"));
                        if (columns.Count >= 6)
                        {
                            var href = columns[6].FindElement(By.TagName("a"));
                            if (href != null)
                            {
                                href.Click();
                                SharedFunctions.sb_waitForReady(this.v_webBrowser);
                                htmlEditPage = this.v_webBrowser.PageSource;
                                string firstName    = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtFirstName");
                                string lastName     = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtLastName");
                                string fatherName   = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtFatherName");
                                string certNo       = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtIdentityNumber");
                                string registerCity = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtIdentityRegisterLocation");
                                string meliNo       = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtNationalCode");

                                string postalCode   = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtAgentPostCode");
                                string email        = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtAgentEmail");
                                string mobileNumber = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtMobileNumber");
                                string birthDate    = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtBirthDate");
                                string address      = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtAgentAddress");

                                if (!string.IsNullOrEmpty(meliNo))
                                {
                                    var entryOwnerExtraDetail = entityLogistic.rwmmsVehicleOwnersDetails.FirstOrDefault(o => o.wMeliNo == meliNo);
                                    if (entryOwnerExtraDetail != null)
                                    {
                                        //entryOwnerExtraDetail.companyId
                                        entryOwnerExtraDetail.CycleNumber   = cycleNumber;
                                        entryOwnerExtraDetail.FetchTime     = DateTime.Now;
                                        entryOwnerExtraDetail.Source        = "http://rwmms.rai.ir/VehicleOwnerDetailInfo.aspx";
                                        entryOwnerExtraDetail.wAddress      = (Functions.IsNull(address) ? null : address);
                                        entryOwnerExtraDetail.wBirthDate    = (Functions.IsNull(birthDate) ? null : birthDate);
                                        entryOwnerExtraDetail.wCertNo       = (Functions.IsNull(certNo) ? null : certNo);
                                        entryOwnerExtraDetail.wEmail        = (Functions.IsNull(email) ? null : email);
                                        entryOwnerExtraDetail.wFatherName   = (Functions.IsNull(fatherName) ? null : fatherName);
                                        entryOwnerExtraDetail.wFName        = (Functions.IsNull(firstName) ? null : firstName);
                                        entryOwnerExtraDetail.wLName        = (Functions.IsNull(lastName) ? null : lastName);
                                        entryOwnerExtraDetail.wMeliNo       = (Functions.IsNull(meliNo) ? null : meliNo);
                                        entryOwnerExtraDetail.wMobileNumber = (Functions.IsNull(mobileNumber) ? null : mobileNumber);
                                        entryOwnerExtraDetail.wPostalCode   = (Functions.IsNull(postalCode) ? null : postalCode);
                                        entryOwnerExtraDetail.wRegisterCity = (Functions.IsNull(registerCity) ? null : registerCity);
                                        entityLogistic.Entry(entryOwnerExtraDetail).State = System.Data.Entity.EntityState.Modified;
                                        entityLogistic.SaveChanges();
                                    }
                                }

                                rowIndex++;
                                goto nextRecord;
                            }
                        }
                    }
                }
            }
        }
        public void sb_readAndSaveToDB(bool loginAndLogout, bool getAttach)
        {
            if (loginAndLogout)
            {
                login lg = new login();
                lg.fnc_logoutWithSelenium(this.v_webBrowser);
                if (login.fnc_isLoginPage(this.v_webBrowser.Url, this.v_url))
                {
                    lg.fnc_loginWithSelenium(this.v_webBrowser);
                    this.v_webBrowser.Navigate().GoToUrl(this.v_url);
                    SharedFunctions.sb_waitForReady(this.v_webBrowser);
                }
            }

            int    pageIndex = 1;
            int    rowIndex;
            string html;

            using (var entityLogistic = new Model.logisticEntities())
            {
                for (pageIndex = 1; pageIndex <= this.v_pageCount; pageIndex++)
                {
                    Program.logs.Info("Start of :" + pageIndex);
                    //if we should get extraDetail we should logout and then login again
                    if (pageIndex > 1)
                    {
                        pageIndex = seleniumDownloader.fnc_gotoPage(this.v_webBrowser, this.v_url, this.v_tableId, pageIndex, this.v_pageCount, false);
                        if (pageIndex == -1)
                        {
                            return;
                        }
                    }

                    html = this.v_webBrowser.PageSource;

                    if (!string.IsNullOrEmpty(html))
                    {
                        this.v_dt.Rows.Clear();
                        Functions.sb_fillDatatableWithHtmlTableId(html, v_tableId, this.v_dt);
                        this.sb_fillDatatablePSID(html, v_tableId, 2, this.v_dt);
                        if (this.v_dt.Rows.Count > 0)
                        {
                            this.sb_saveGroupsToDB(this.v_dt, null);
                        }
                    }
                    else
                    {
                        break;
                    }

                    try
                    {
                        int groupId;
                        for (rowIndex = 0; rowIndex <= this.v_dt.Rows.Count - 1; rowIndex++)
                        {
                            if (Functions.IsNull(this.v_dt.Rows[rowIndex][wagonPartsGroupsDataTable.fld_id]))
                            {
                                continue;
                            }

                            groupId = int.Parse(this.v_dt.Rows[rowIndex][wagonPartsGroupsDataTable.fld_id].ToString());

                            var el    = this.v_webBrowser.FindElement(By.Id(this.v_tableId));
                            var rows  = el.FindElements(By.TagName("tr"));
                            var cells = rows[rowIndex + this.v_dt.prp_skipRowTop].FindElements(By.TagName("td"));
                            cells[2].Click();
                            SharedFunctions.sb_waitForReady(this.v_webBrowser);

                            var queryString = HttpUtility.ParseQueryString(this.v_webBrowser.Url);
                            if (!Functions.IsNull(queryString["psid"]))
                            {
                                var entryPartGroup = entityLogistic.rwmmsWagonPartsGroups.FirstOrDefault(o => o.Id == groupId);
                                if (entryPartGroup != null)
                                {
                                    entryPartGroup.wPSID = queryString["psid"];
                                    entityLogistic.SaveChanges();
                                }
                            }
                            this.sb_readAndSavePartsToDB(groupId, this.v_dt.Rows[rowIndex][wagonPartsGroupsDataTable.fld_groupName].ToString(), getAttach
                                                         , this.v_downloadPath);
                            this.sb_readAndSaveSubGroupsToDB(groupId, getAttach, this.v_downloadPath + (this.v_downloadPath.EndsWith("\\") ? "" : "\\") + this.v_dt.Rows[rowIndex][wagonPartsGroupsDataTable.fld_groupName].ToString());

                            var elPath         = this.v_webBrowser.FindElement(By.Id(this.v_pathElementId));
                            var hrefCollection = elPath.FindElements(By.TagName("a"));
                            if (hrefCollection != null && hrefCollection.Count > 0)
                            {
                                hrefCollection[hrefCollection.Count - 1].Click();
                                SharedFunctions.sb_waitForReady(this.v_webBrowser);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    Program.logs.Info("End of :" + pageIndex);
                }
            }
        }
        private void sb_getAttach(wagonPartsDataTable dt, string directoryPath, string groupName)
        {
            System.Collections.ObjectModel.ReadOnlyCollection <IWebElement> rows;
            int htmlRowIndex = dt.prp_skipRowTop;
            int dtRowIndex   = 0;
            int fileExistCounter;

            char[] chArrInvalidFileChars = Path.GetInvalidFileNameChars();
            char[] chArrInvalidPathChars = Path.GetInvalidPathChars();
            groupName = string.Join("_", groupName.Split(chArrInvalidFileChars));
            groupName = string.Join("_", groupName.Split(chArrInvalidPathChars));

            string downloadPath = directoryPath + (directoryPath.EndsWith("\\") ? "" : "\\")
                                  + groupName;

            if (!Directory.Exists(downloadPath))
            {
                Directory.CreateDirectory(downloadPath);
            }

            do
            {
                try
                {
                    rows = Functions.fnc_getSeleniumTableRows(this.v_webBrowser, this.v_tableIdParts);
                }
                catch
                {
                    return;
                }
                if (!Functions.IsNull(dt.Rows[dtRowIndex][wagonPartsDataTable.fld_partName]))
                {
                    var cells = rows[htmlRowIndex].FindElements(By.TagName("td"));
                    if (cells.Count >= 6)
                    {
                        if (!Functions.IsNull(cells[6].Text))
                        {
                            cells[6].Click();
                            SharedFunctions.sb_waitForReady(this.v_webBrowser);

                            FileInfo myFile;
                            do
                            {
                                fileExistCounter = 0;
                                myFile           = (new DirectoryInfo(this.v_downloadPath)).GetFiles("*.*").OrderByDescending(o => o.LastWriteTime).FirstOrDefault();
                                #region wait for download completion
                                while (myFile == null || myFile.LastWriteTime < DateTime.Now.AddMinutes(-5))
                                {
                                    Thread.Sleep(1000);
                                    myFile = (new DirectoryInfo(this.v_downloadPath)).GetFiles("*.*").OrderByDescending(o => o.LastWriteTime).FirstOrDefault();
                                    fileExistCounter++;
                                    if (fileExistCounter > 30)
                                    {
                                        break;
                                    }
                                }
                                #endregion

                                if (myFile == null || myFile.LastWriteTime < DateTime.Now.AddMinutes(-5))
                                {
                                    //there is error
                                    throw new Exception("File cannot be downloaded");
                                }
                                else if (myFile.Extension != ".crdownload" && myFile.Extension != ".tmp")
                                {
                                    string fileName = "map_" + dt.Rows[dtRowIndex][wagonPartsDataTable.fld_partName].ToString();

                                    fileName = string.Join("_", fileName.Split(chArrInvalidFileChars));
                                    fileName = string.Join("_", fileName.Split(chArrInvalidPathChars));
                                    fileName = downloadPath + "\\" + fileName + myFile.Extension;
                                    try
                                    {
                                        if (File.Exists(fileName))
                                        {
                                            File.Delete(fileName);
                                        }

                                        myFile.MoveTo(fileName);

                                        dt.Rows[dtRowIndex][wagonPartsDataTable.fld_mapPicPath] = fileName;
                                    }
                                    catch (Exception ex)
                                    {
                                    }
                                    break;
                                }
                            } while (myFile.Extension == ".crdownload" || myFile.Extension == ".tmp" /*wait till .crdownload or .tmp to change to .pdf*/);
                        }
                    }
                }
                htmlRowIndex++;
                dtRowIndex++;
            } while (htmlRowIndex < rows.Count - dt.prp_skipRowBottom);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="webBrowser"></param>
        /// <param name="url"></param>
        /// <param name="tableId"></param>
        /// <param name="controlIdToClick">to get pagecount in some pages we need to click a control if this parameter is specified
        /// this function click the control</param>
        /// <param name="logoutAndLogin">for some pages to get all rows we need logoutAndLogin set to true</param>
        /// <returns></returns>
        public static int fnc_getPageCount(IWebDriver webBrowser, string url, By tableBy, string controlIdToClick, bool logoutAndLogin)
        {
            login lg = new login();

            if (logoutAndLogin)
            {
                lg.fnc_logoutWithSelenium(webBrowser);
            }
            if (login.fnc_isLoginPage(webBrowser.Url, url))
            {
                lg.fnc_loginWithSelenium(webBrowser);
                webBrowser.Navigate().GoToUrl(url);
            }

            if (webBrowser.Url.ToLower() != url.ToLower())
            {
                webBrowser.Navigate().GoToUrl(url);
                SharedFunctions.sb_waitForReady(webBrowser);
            }
            if (!string.IsNullOrEmpty(controlIdToClick))
            {
                sb_click(webBrowser, url, controlIdToClick, false);
            }
            int lastPageIndex = -1;
            int temp, i;

            getPageSource : string html = webBrowser.PageSource;
            var table = webBrowser.FindElement(tableBy);

            //int paginationGroup = 0;
            if (table != null)
            {
                List <IWebElement> rows;
                try
                {
                    var tbody = table.FindElement(By.TagName("tbody"));
                    if (tbody != null)
                    {
                        rows = tbody.FindElements(By.TagName("tr")).ToList();
                    }
                    else
                    {
                        rows = table.FindElements(By.TagName("tr")).ToList();
                    }
                }
                catch
                {
                    rows = table.FindElements(By.TagName("tr")).ToList();
                }

                if (rows != null && rows.Count > 0)
                {
                    //we have atleast one page
                    lastPageIndex = 1;
                    var lastRow        = rows[rows.Count - 1];
                    var td             = lastRow.FindElement(By.TagName("td"));
                    var hrefCollection = td.FindElements(By.TagName("a"));
                    if (hrefCollection != null && hrefCollection.Count > 0)
                    {
                        if (/*v_nextPaginationGroupPos <= hrefCollection.Count
                             * &&*/hrefCollection[hrefCollection.Count - 1].Text == v_nextPaginationText)
                        {
                            //we have next page so we should have next paginationgroup
                            hrefCollection[hrefCollection.Count - 1].Click();
                            SharedFunctions.sb_waitForReady(webBrowser);
                            goto getPageSource;
                            //paginationGroup++;
                        }
                        else
                        {
                            //we do not have nextPaginationGroup
                            for (i = 0; i <= hrefCollection.Count - 1; i++)
                            {
                                if (int.TryParse(hrefCollection[i].Text, out temp))
                                {
                                    if (temp > lastPageIndex)
                                    {
                                        lastPageIndex = temp;
                                    }
                                }
                            }
                            return(lastPageIndex);
                        }
                        //if (hrefCollection.Count < v_paginationSize)
                        //{
                        //    //we do not have next paginiation for example we have only 3 pages and paginzationsize is 10
                        //    var pageIndexStr = hrefCollection[hrefCollection.Count - 1].Text;
                        //    int temp;
                        //    if (int.TryParse(pageIndexStr, out temp))
                        //    {
                        //        lastPageIndex = temp + (paginationGroup * v_paginationSize);
                        //    }
                        //}
                        //else if (hrefCollection.Count > v_paginationSize)
                        //{
                        //    //we have more pages than paginzationsize. e.g. we have 12 pages while paginationsize is 10
                        //    hrefCollection[v_nextPaginationGroupPos].Click();
                        //    SharedFunctions.sb_waitForReady(webBrowser);
                        //    paginationGroup++;

                        //}
                        //else if (hrefCollection.Count == v_paginationSize)
                        //{
                        //    //there are three possibilities paginationsize = 10 and we have 10 pages
                        //    //or we have 19 pages in second group we will have 9 pages and previous button
                        //    //or we have 9 pages  + nextPagination we have 10 hrefCollection
                        //    //so => hrefCollection.Count == v_paginationSize
                        //    if (hrefCollection[v_nextPaginationGroupPos].Text == v_nextPaginationText)
                        //    {
                        //        hrefCollection[v_nextPaginationGroupPos].Click();
                        //        SharedFunctions.sb_waitForReady(webBrowser);
                        //        paginationGroup++;
                        //    }
                        //    else
                        //    {
                        //        var pageIndexStr = hrefCollection[hrefCollection.Count - 1].Text;
                        //        int temp;
                        //        if (int.TryParse(pageIndexStr, out temp))
                        //        {
                        //            lastPageIndex = temp + (paginationGroup * v_paginationSize);
                        //        }
                        //    }
                        //}
                    }
                }
            }
            return(lastPageIndex);
        }
        public static int fnc_gotoPage(IWebDriver webBrowser, string url, By tableBy, int pageIndex, int pageCount, bool logoutAndLogin
                                       , bool waitTillReady = true)
        {
            int i, temp, max, min;

            min = int.MaxValue;
            max = int.MinValue;
            login lg = new login();

            if (logoutAndLogin)
            {
                lg.fnc_logoutWithSelenium(webBrowser);
            }
            if (login.fnc_isLoginPage(webBrowser.Url, url))
            {
                lg.fnc_loginWithSelenium(webBrowser);
                webBrowser.Navigate().GoToUrl(url);
            }

            if (webBrowser.Url.ToLower() != url.ToLower())
            {
                webBrowser.Navigate().GoToUrl(url);
                SharedFunctions.sb_waitForReady(webBrowser);
            }

            if (pageCount == -1)
            {
                pageCount = fnc_getPageCount(webBrowser, url, tableBy, false);
            }
            if (pageIndex > pageCount)
            {
                return(-1);
            }

            if (pageIndex == 1)
            {
                webBrowser.Navigate().GoToUrl(url);
                if (waitTillReady)
                {
                    SharedFunctions.sb_waitForReady(webBrowser);
                }
                return(pageIndex);
            }
            else
            {
                //webBrowser.Navigate().GoToUrl(url);
                //SharedFunctions.sb_waitForReady(webBrowser);
                var table = webBrowser.FindElement(tableBy);
                if (table != null)
                {
                    var rows = table.FindElements(By.TagName("tr"));
                    if (rows != null && rows.Count > 0)
                    {
                        var lastRow = rows[rows.Count - 1];
                        var td      = lastRow.FindElement(By.TagName("td"));
                        if (td != null)
                        {
                            var hrefCollection = td.FindElements(By.TagName("a")).ToList();
                            var hrefCollectionAndCurrentPage = hrefCollection.ToList();
                            hrefCollectionAndCurrentPage.AddRange(td.FindElements(By.TagName("span")).ToList());

                            var hrefPage = hrefCollectionAndCurrentPage.FirstOrDefault(o => o.Text == pageIndex.ToString());
                            if (hrefPage != null)
                            {
                                hrefPage.Click();
                                if (waitTillReady)
                                {
                                    SharedFunctions.sb_waitForReady(webBrowser);
                                }
                                return(pageIndex);
                            }
                            else
                            {
                                for (i = 0; i <= hrefCollectionAndCurrentPage.Count - 1; i++)
                                {
                                    if (int.TryParse(hrefCollectionAndCurrentPage[i].Text, out temp))
                                    {
                                        if (temp < min)
                                        {
                                            min = temp;
                                        }
                                        if (temp > max)
                                        {
                                            max = temp;
                                        }
                                    }
                                }
                                if (min == int.MaxValue || max == int.MinValue)
                                {
                                    //we cannot find any href with integer text
                                    return(-1);
                                }
                                if (min <= pageIndex && pageIndex <= max)
                                {
                                    //page index is between min and max but we could find the page in previous step so there is a problem
                                    return(-1);
                                }
                                if (pageIndex < min)
                                {
                                    //go to previous page
                                    if (hrefCollection.Count >= v_previousPaginationGroupPos &&
                                        hrefCollection[v_previousPaginationGroupPos].Text == v_previousPaginationText)
                                    {
                                        hrefCollection[v_previousPaginationGroupPos].Click();
                                        if (waitTillReady)
                                        {
                                            SharedFunctions.sb_waitForReady(webBrowser);
                                        }

                                        return(fnc_gotoPage(webBrowser, url, tableBy, pageIndex, pageCount, logoutAndLogin));
                                    }
                                }
                                else if (pageIndex > max)
                                {
                                    //go to next page
                                    if (/*hrefCollection.Count >= v_nextPaginationGroupPos
                                         * &&*/hrefCollection[hrefCollection.Count - 1].Text == v_nextPaginationText)
                                    {
                                        hrefCollection[hrefCollection.Count - 1].Click();
                                        if (waitTillReady)
                                        {
                                            SharedFunctions.sb_waitForReady(webBrowser);
                                        }

                                        return(fnc_gotoPage(webBrowser, url, tableBy, pageIndex, pageCount, logoutAndLogin));
                                    }
                                }

                                return(-1);
                            }
                        }
                        else
                        {
                            return(-1);
                        }
                    }
                    else
                    {
                        return(-1);
                    }
                }
                else
                {
                    return(-1);
                }
            }
        }
Esempio n. 10
0
        private void sb_readOwnerDetail(int companyId, int cycleNumber)
        {
            login lg = new login();

            lg.fnc_loginWithSelenium(this.v_webBrowser);

            bool   add;
            string tableId = "ContentPlaceHolder1_dgVehicleOwners";

            this.v_webBrowser.Navigate().GoToUrl(this.v_url);
            SharedFunctions.sb_waitForReady(this.v_webBrowser);
            var btnSearch = this.v_webBrowser.FindElement(By.Id("ContentPlaceHolder1_btnSearch"));

            if (btnSearch == null)
            {
                return;
            }
            btnSearch.Click();
            SharedFunctions.sb_waitForReady(this.v_webBrowser);
            var table = this.v_webBrowser.FindElement(By.Id(tableId));

            if (table == null)
            {
                return;
            }
            var rows = table.FindElements(By.TagName("tr"));

            if (rows == null || rows.Count < 3)
            {
                return;
            }
            var columns = rows[1].FindElements(By.TagName("td"));

            if (columns.Count >= 6)
            {
                var href = columns[6].FindElement(By.TagName("a"));
                if (href != null)
                {
                    href.Click();
                    SharedFunctions.sb_waitForReady(this.v_webBrowser);
                    table = this.v_webBrowser.FindElement(By.Id("ContentPlaceHolder1_dgVehicleOwnerDetails"));
                    if (table == null)
                    {
                        return;
                    }
                    rows = table.FindElements(By.TagName("tr"));
                    if (rows == null || rows.Count < 3)
                    {
                        return;
                    }
                    columns = rows[1].FindElements(By.TagName("td"));
                    if (columns.Count >= 6)
                    {
                        href = columns[6].FindElement(By.TagName("a"));
                        if (href != null)
                        {
                            href.Click();
                            //SharedFunctions.sb_waitForReady(this.v_webBrowser);
                            SharedFunctions.sb_waitForReady(this.v_webBrowser);
                            string htmlEditPage = this.v_webBrowser.PageSource;
                            string firstName    = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtFirstName");
                            string lastName     = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtLastName");
                            string fatherName   = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtFatherName");
                            string certNo       = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtIdentityNumber");
                            string registerCity = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtIdentityRegisterLocation");
                            string meliNo       = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtNationalCode");

                            string postalCode   = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtAgentPostCode");
                            string email        = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtAgentEmail");
                            string mobileNumber = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtMobileNumber");
                            string birthDate    = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtBirthDate");
                            string address      = Functions.fnc_getElementValue(htmlEditPage, "ContentPlaceHolder1_txtAgentAddress");
                            using (var entityLogistic = new Model.logisticEntities())
                            {
                                if (!string.IsNullOrEmpty(meliNo))
                                {
                                    add = false;
                                    var entryOwnerExtraDetail = entityLogistic.rwmmsVehicleOwnersDetails.FirstOrDefault(o => o.wMeliNo == meliNo);
                                    if (entryOwnerExtraDetail == null)
                                    {
                                        entryOwnerExtraDetail = new Model.rwmmsVehicleOwnersDetail();
                                        add = true;
                                    }

                                    entryOwnerExtraDetail.CycleNumber    = cycleNumber;
                                    entryOwnerExtraDetail.FetchTime      = DateTime.Now;
                                    entryOwnerExtraDetail.Source         = this.v_url;
                                    entryOwnerExtraDetail.vehicleOwnerId = companyId;
                                    entryOwnerExtraDetail.wAddress       = (Functions.IsNull(address) ? null : address);
                                    entryOwnerExtraDetail.wBirthDate     = (Functions.IsNull(birthDate) ? null : birthDate);
                                    entryOwnerExtraDetail.wCertNo        = (Functions.IsNull(certNo) ? null : certNo);
                                    entryOwnerExtraDetail.wEmail         = (Functions.IsNull(email) ? null : email);
                                    entryOwnerExtraDetail.wFatherName    = (Functions.IsNull(fatherName) ? null : fatherName);
                                    entryOwnerExtraDetail.wFName         = (Functions.IsNull(firstName) ? null : firstName);
                                    entryOwnerExtraDetail.wLName         = (Functions.IsNull(lastName) ? null : lastName);
                                    entryOwnerExtraDetail.wMeliNo        = (Functions.IsNull(meliNo) ? null : meliNo);
                                    entryOwnerExtraDetail.wMobileNumber  = (Functions.IsNull(mobileNumber) ? null : mobileNumber);
                                    entryOwnerExtraDetail.wPostalCode    = (Functions.IsNull(postalCode) ? null : postalCode);
                                    entryOwnerExtraDetail.wRegisterCity  = (Functions.IsNull(registerCity) ? null : registerCity);
                                    if (add)
                                    {
                                        entityLogistic.rwmmsVehicleOwnersDetails.Add(entryOwnerExtraDetail);
                                    }
                                    else
                                    {
                                        entityLogistic.Entry(entryOwnerExtraDetail).State = System.Data.Entity.EntityState.Modified;
                                    }
                                    entityLogistic.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }
        }
        public void sb_readAndSaveToDB(int cycleNumber, string ownerName, string typeName, bool getLastItems, bool getDetail, bool getAttach, bool loginAndLogout)
        {
            if (loginAndLogout)
            {
                login lg = new login();
                lg.fnc_logoutWithSelenium(this.v_webBrowser);
                if (login.fnc_isLoginPage(this.v_webBrowser.Url, this.v_url))
                {
                    lg.fnc_loginWithSelenium(this.v_webBrowser);
                    this.v_webBrowser.Navigate().GoToUrl(this.v_url);
                    seleniumDownloader.sb_click(this.v_webBrowser, this.v_url, this.v_buttonSearchId, false);
                    SharedFunctions.sb_waitForReady(this.v_webBrowser);
                }
            }

            int    pageIndex = 1;
            string html;
            int    pageIndexStart = 1;

            if (getLastItems)
            {
                using (var entityLogistic = new Model.logisticEntities())
                {
                    int?maxPageNo = entityLogistic.rwmmsDeclerationLists.Where(o => o.wOwnerName == ownerName && o.wTypeName == typeName).Max(o => o.PageIndex);
                    if (maxPageNo.HasValue)
                    {
                        pageIndexStart = maxPageNo.Value;
                    }
                    else
                    {
                        pageIndexStart = 1;
                    }
                }
            }
            for (pageIndex = pageIndexStart; pageIndex <= this.v_pageCount; pageIndex++)
            {
                Program.logs.Info("Start of :" + pageIndex);
                //if we should get extraDetail we should logout and then login again
                if (pageIndex > 1)
                {
                    pageIndex = seleniumDownloader.fnc_gotoPage(this.v_webBrowser, this.v_url, this.v_tableId, pageIndex, this.v_pageCount, false);
                    if (pageIndex == -1)
                    {
                        return;
                    }
                }

                html = this.v_webBrowser.PageSource;

                if (!string.IsNullOrEmpty(html))
                {
                    this.v_dt.Rows.Clear();
                    Functions.sb_fillDatatableWithHtmlTableId(html, v_tableId, this.v_dt);
                    if (this.v_dt.Rows.Count > 0)
                    {
                        if (getDetail)
                        {
                            System.Collections.ObjectModel.ReadOnlyCollection <IWebElement> rows;
                            int i = this.v_dt.prp_skipRowTop;
                            do
                            //for (int i = this.v_dt.prp_skipRowTop; i <= rows.Count - this.v_dt.prp_skipRowBottom; i++)
                            {
                                rows = Functions.fnc_getSeleniumTableRows(this.v_webBrowser, this.v_tableId);
                                var cells = rows[i].FindElements(By.TagName("td"));
                                if (cells.Count >= 8)
                                {
                                    cells[8].Click();
                                    SharedFunctions.sb_waitForReady(this.v_webBrowser);
                                    Functions.sb_fillDataRowWithHtmlControls(this.v_webBrowser.PageSource, this.v_dt, this.v_dt.Rows[i - this.v_dt.prp_skipRowTop]);
                                    try
                                    {
                                        var btnCancel = this.v_webBrowser.FindElement(By.Id("ContentPlaceHolder1_cmdCancel"));
                                        btnCancel.Click();
                                        SharedFunctions.sb_waitForReady(this.v_webBrowser);
                                    }
                                    catch
                                    {
                                        return;
                                    }
                                }
                                i++;
                            } while (i < rows.Count - this.v_dt.prp_skipRowBottom);
                        }

                        if (getAttach)
                        {
                            this.sb_getAttach();
                        }
                        this.sb_saveToDB(this.v_dt, cycleNumber, pageIndex, ownerName, typeName, getDetail, getAttach);
                    }
                }
                else
                {
                    break;
                }
                Program.logs.Info("End of :" + pageIndex);
            }
        }
Esempio n. 12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="webBrowser"></param>
        /// <param name="url"></param>
        /// <param name="grid"></param>
        /// <param name="controlIdToClick">to get pagecount in some pages we need to click a control if this parameter is specified
        /// this function click the control</param>
        /// <param name="logoutAndLogin">for some pages to get all rows we need logoutAndLogin set to true</param>
        /// <returns></returns>
        public static int fnc_getPageCount(IWebDriver webBrowser, string url, IWebElement grid
                                           , IWebElement rowCountElement, string rowCountLabelRegexPattern, int pageRowCount, bool logoutAndLogin)
        {
            if (rowCountElement != null)
            {
                var rowCountStr = rowCountElement.Text;
                int rowCount;
                if (!int.TryParse(rowCountStr, out rowCount))
                {
                    if (!string.IsNullOrEmpty(rowCountLabelRegexPattern))
                    {
                        rowCountStr = System.Text.RegularExpressions.Regex.Replace(rowCountStr, rowCountLabelRegexPattern, "");
                    }
                    rowCountStr = rowCountStr.Replace("&nbsp;", "");
                    rowCountStr = rowCountStr.Replace(" ", "");
                    rowCountStr = rowCountStr.Replace(",", "");
                    rowCountStr = rowCountStr.Replace("تعدادرکورد", "");
                    rowCountStr = rowCountStr.Replace("تعدادركورد", "");
                    rowCountStr = rowCountStr.TrimStart().TrimEnd();
                    rowCount    = int.Parse(rowCountStr);
                }
                return(rowCount / pageRowCount + (rowCount % pageRowCount > 0 ? 1 : 0));
            }
            else
            {
                login lg = new login();
                if (logoutAndLogin)
                {
                    lg.fnc_logoutWithSelenium(webBrowser);
                }
                if (login.fnc_isLoginPage(webBrowser.Url, url))
                {
                    lg.fnc_loginWithSelenium(webBrowser);
                    webBrowser.Navigate().GoToUrl(url);
                }

                if (webBrowser.Url.ToLower() != url.ToLower())
                {
                    webBrowser.Navigate().GoToUrl(url);
                    SharedFunctions.sb_waitForReady(webBrowser);
                }

                int lastPageIndex = -1;
                int temp, i;
                getPageSource : string html = webBrowser.PageSource;

                //int paginationGroup = 0;
                if (grid != null)
                {
                    List <IWebElement> rows;
                    try
                    {
                        var tbody = grid.FindElement(By.TagName("tbody"));
                        if (tbody != null)
                        {
                            rows = tbody.FindElements(By.TagName("tr")).ToList();
                        }
                        else
                        {
                            rows = grid.FindElements(By.TagName("tr")).ToList();
                        }
                    }
                    catch
                    {
                        rows = grid.FindElements(By.TagName("tr")).ToList();
                    }

                    if (rows != null && rows.Count > 0)
                    {
                        //we have atleast one page
                        lastPageIndex = 1;
                        var lastRow        = rows[rows.Count - 1];
                        var td             = lastRow.FindElement(By.TagName("td"));
                        var hrefCollection = td.FindElements(By.TagName("a"));
                        if (hrefCollection != null && hrefCollection.Count > 0)
                        {
                            if (/*v_nextPaginationGroupPos <= hrefCollection.Count
                                 * &&*/hrefCollection[hrefCollection.Count - 1].Text == v_nextPaginationText)
                            {
                                //we have next page so we should have next paginationgroup
                                hrefCollection[hrefCollection.Count - 1].Click();
                                SharedFunctions.sb_waitForReady(webBrowser);
                                goto getPageSource;
                                //paginationGroup++;
                            }
                            else
                            {
                                //we do not have nextPaginationGroup
                                for (i = 0; i <= hrefCollection.Count - 1; i++)
                                {
                                    if (int.TryParse(hrefCollection[i].Text, out temp))
                                    {
                                        if (temp > lastPageIndex)
                                        {
                                            lastPageIndex = temp;
                                        }
                                    }
                                }
                                return(lastPageIndex);
                            }
                        }
                    }
                }
                return(lastPageIndex);
            }
        }
Esempio n. 13
0
        /// <summary>
        /// set the prp_datatable
        /// </summary>
        /// <param name="getDetail"></param>
        /// <param name="download"></param>
        /// <param name="startPageIndex">read data from startPageIndex</param>
        private void sb_setDataSource(bool getDetail, bool download, int startPageIndex)
        {
            if (this.prp_controlLoadDataBy != null)
            {
                this.prp_webBrowser.FindElement(this.prp_controlLoadDataBy).Click();
                SharedFunctions.sb_waitForReady(this.prp_webBrowser);
            }
            var element = this.fnc_getHtmlElement();

            this.prp_pageCount = webpageGrid.fnc_getPageCount(this.prp_webBrowser, this.prp_webpage.prp_url, element, this.fnc_getRowCountHtmlElement(), null, this.prp_pageRowCount, false);

            int pageIndex;
            int i, j;

            DataTable dt = this.fnc_createDatatable();
            DataRow   dr;
            webpageGridColumnDownload columnDownload;
            webpageGridColumnDetail   columnDetail;
            webpage pgDetail;
            string  val;

            for (pageIndex = startPageIndex; pageIndex <= this.prp_pageCount; pageIndex++)
            {
                if (pageIndex > 1)
                {
                    pageIndex = webpageGrid.fnc_gotoPage(this.prp_webBrowser, this.prp_webpage.prp_url, element, this.fnc_getRowCountHtmlElement(), pageIndex, this.prp_pageCount, false
                                                         , this.prp_controlRowCountLabelRegexPattern, this.prp_pageRowCount, false);
                    if (pageIndex == -1)
                    {
                        return;
                    }
                }
                element = this.fnc_getHtmlElement();


                var htmlRows = element.FindElements(By.TagName("tr"));
                System.Collections.ObjectModel.ReadOnlyCollection <IWebElement> htmlCells;

                for (i = this.prp_skipRowTop; i <= htmlRows.Count - 1 - this.prp_skipRowBottom; i++)
                {
                    if (this.prp_skipRowIndecies != null && this.prp_skipRowIndecies.Any(o => o == i))
                    {
                        continue;
                    }

                    dr        = null;
                    htmlCells = htmlRows[i].FindElements(By.TagName("td"));
                    for (j = 0; j <= this.prp_gridColumns.Length - 1; j++)
                    {
                        if (this.prp_gridColumns[j] is webpageGridColumnDetail)
                        {
                            if (getDetail)
                            {
                                columnDetail = ((webpageGridColumnDetail)this.prp_gridColumns[j]);
                                pgDetail     = (webpage)Activator.CreateInstance(columnDetail.prp_webpgType);
                                this.sb_gotoDetail(htmlCells[columnDetail.prp_colIndexInGrid]
                                                   , pgDetail, columnDetail.prp_getDetail, columnDetail.prp_download
                                                   , columnDetail.prp_startPageIndex
                                                   , columnDetail.prp_controlLoadDataBy);
                                dr[this.prp_gridColumns[j].prp_colName] = pgDetail;
                            }
                        }
                        else if (this.prp_gridColumns[j] is webpageGridColumnDownload)
                        {
                            if (download)
                            {
                                columnDownload = ((webpageGridColumnDownload)this.prp_gridColumns[j]);
                                dr[this.prp_gridColumns[j].prp_colName] = this.fnc_downloadAttach(htmlCells[columnDownload.prp_colIndexInGrid]
                                                                                                  , columnDownload.prp_downloadDirectory
                                                                                                  , columnDownload.prp_fileDirPath
                                                                                                  , columnDownload.fnc_getFileName(htmlCells));
                            }
                        }
                        else
                        {
                            if (dr == null)
                            {
                                dr = dt.NewRow();
                            }
                            val = htmlCells[this.prp_gridColumns[j].prp_colIndexInGrid].Text.Replace("&nbsp;", " ").TrimStart().TrimEnd();
                            if (string.IsNullOrEmpty(val))
                            {
                                dr[this.prp_gridColumns[j].prp_colName] = DBNull.Value;
                            }
                            else
                            {
                                dr[this.prp_gridColumns[j].prp_colName] = Convert.ChangeType(val, this.prp_gridColumns[j].prp_datatype);
                            }
                        }
                    }
                    if (dr != null)
                    {
                        dt.Rows.Add(dr);
                    }
                }
            }
            this.prp_datasource = dt;
        }