Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Created January 6, 2017 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelovstanton/0a9b24ea8a9a09e4fb224a94f0fc4938 to your computer and use it in GitHub Desktop.
Save angelovstanton/0a9b24ea8a9a09e4fb224a94f0fc4938 to your computer and use it in GitHub Desktop.
public partial class ArticlesPage : BasePage
{
private string viewsRegex = @".*Views: (?<Views>[0-9,]{1,})";
private string publishDateRegex = @".*Posted: (?<PublishDate>[0-9,A-Za-z ]{1,})";
private readonly int profileId;
public ArticlesPage(IWebDriver driver, int profileId) : base(driver)
{
this.profileId = profileId;
}
public override string Url
{
get
{
return string.Format("https://www.codeproject.com/script/Articles/MemberArticles.aspx?amid={0}", this.profileId);
}
}
public void Navigate(string part)
{
base.Open(part);
}
public List<Article> GetArticlesByUrl(string sectionPart)
{
this.Navigate(sectionPart);
var articlesInfos = new List<Article>();
foreach (var articleRow in this.ArticlesRows.ToList())
{
if (!articleRow.Displayed)
{
continue;
}
var article = new Article();
var articleTitleElement = this.GetArticleTitleElement(articleRow);
article.Title = articleTitleElement.GetAttribute("innerHTML");
article.Url = articleTitleElement.GetAttribute("href");
var articleStatisticsElement = this.GetArticleStatisticsElement(articleRow);
string articleStatisticsElementSource = articleStatisticsElement.GetAttribute("innerHTML");
if (!string.IsNullOrEmpty(articleStatisticsElementSource))
{
article.Views = this.GetViewsCount(articleStatisticsElementSource);
article.PublishDate = this.GetPublishDate(articleStatisticsElementSource);
}
articlesInfos.Add(article);
}
return articlesInfos;
}
private double GetViewsCount(string articleStatisticsElementSource)
{
var regexViews = new Regex(viewsRegex, RegexOptions.Singleline);
Match currentMatch = regexViews.Match(articleStatisticsElementSource);
if (!currentMatch.Success)
{
throw new ArgumentException("No content for the current statistics element.");
}
return double.Parse(currentMatch.Groups["Views"].Value);
}
private DateTime GetPublishDate(string articleStatisticsElementSource)
{
var regexPublishDate = new Regex(publishDateRegex, RegexOptions.IgnorePatternWhitespace);
Match currentMatch = currentMatch = regexPublishDate.Match(articleStatisticsElementSource);
if (!currentMatch.Success)
{
throw new ArgumentException("No content for the current statistics element.");
}
return DateTime.Parse(currentMatch.Groups["PublishDate"].Value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment