using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace WhatsAppConsole
{
public class WhatsAppConsoleProgram
{
IWebDriver driver;
static void Main(string[] args)
{
WhatsAppConsoleProgram pro = new WhatsAppConsoleProgram();
pro.ReadJSONData(@"\TestData\package.json");
}
public void ReadJSONData(string jsonFilePath)
{
driver = new ChromeDriver(@"\SeleniumDrivers\");
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://web.whatsapp.com/");
while (true)
{
Thread.Sleep(1000);
if (isInSecondPage())
{
Thread.Sleep(15000);
this.moveToLogedInWindow();
}
}
}
private bool isInSecondPage()
{
try
{
if (driver.FindElement(By.XPath("//*[@id='window']/div[1]/div[1]/div/img")).Displayed == true)
return false;
}catch (Exception ex){ }
return true;
}
private void moveToLogedInWindow()
{
StreamReader file = File.OpenText(@"\TestData\package.json");
JsonTextReader reader = new JsonTextReader(file);
JObject o2 = (JObject)JToken.ReadFrom(reader);
IList<JToken> results = o2["data"].Children().ToList();
foreach (JToken result in results)
{
Thread.Sleep(5000);
string user = result["searchUser"].ToString();
string type = result["Advance"]["Type"].ToString();
string fileName = string.Empty;
if (result["Advance"]["FileName"] != null)
fileName = result["Advance"]["FileName"].ToString();
string caption = result["Advance"]["Caption"].ToString();
try
{
this.SendMSGToMultipleUsers(user, type, caption, @"\TestData\" + fileName);
//Need to update sent Status
}
catch (Exception ex)
{
// //Need to update failure Status
// Update the Admin
}
}
}
public void SendMSGToMultipleUsers(string searchUser, string attachType, string postCaption, string fileName)
{
try
{
IWebElement txt_Search = driver.FindElement(By.XPath(".//*[@id='side']//input[@class='input input-search']"));
txt_Search.Click();
txt_Search.SendKeys(searchUser);
Thread.Sleep(5000);
IWebElement selectUsr_Click = driver.FindElement(By.XPath(".//*[@id='pane-side']//div[@class='chat-body']"));
selectUsr_Click.Click();
Thread.Sleep(5000);
switch (attachType)
{
case "Message":
SendMessage(postCaption);
break;
case "Image":
sendFile(attachType, postCaption, fileName);
break;
case "Document":
sendFile(attachType, postCaption, fileName);
break;
}
}
catch (Exception ex)
{
throw ex;
}
}
private void sendFile(string attachType, string postCaption, string fileName)
{
IWebElement link_Attach = link_Attach = driver.FindElement(By.XPath(".//*[@id='main']//button[@class='icon icon-clip']"));
link_Attach.Click();
Thread.Sleep(3000);
IWebElement link_Gallery = driver.FindElement(By.XPath(".//*[@id='main']/header/div[3]/div/div[2]/span/div/div/ul/li[1]/button"));
if (attachType == "Document")
link_Gallery = driver.FindElement(By.XPath(".//*[@id='main']/header/div[3]/div/div[2]/span/div/div/ul/li[3]/button"));
link_Gallery.Click();
Thread.Sleep(3000);
this.FileUpload(link_Gallery, fileName);
if (attachType != "Document")
{
IWebElement txt_InputCaption = driver.FindElement(By.XPath(".//*[@id='app']//div[@class='input input-text']"));
txt_InputCaption.Click();
txt_InputCaption.SendKeys(postCaption);
Thread.Sleep(500);
IWebElement btn_ClickSend = driver.FindElement(By.XPath(".//*[@id='app']//button[@class='btn btn-round btn-l']"));
btn_ClickSend.Click();
}
else
{
IWebElement btn_ClickSend = driver.FindElement(By.XPath(".//*[@id='app']//button[@class='btn btn-round btn-l']"));
btn_ClickSend.Click();
Thread.Sleep(3000);
SendMessage(postCaption);
}
}
private void SendMessage(string message)
{
IWebElement txt_Input = driver.FindElement(By.XPath(".//*[@id='main']//div[@class='input']"));
txt_Input.Click();
txt_Input.SendKeys(message);
Thread.Sleep(3000);
IWebElement btn_Text = driver.FindElement(By.XPath(".//*[@id='main']//button[@class='icon btn-icon icon-send send-container']"));
btn_Text.Click();
Thread.Sleep(5000);
}
public void FileUpload(IWebElement controlGallery, string fileName = null)
{
System.Windows.Forms.SendKeys.SendWait(fileName);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Thread.Sleep(5000);
}
}
}
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace WhatsAppConsole
{
public class WhatsAppConsoleProgram
{
IWebDriver driver;
static void Main(string[] args)
{
WhatsAppConsoleProgram pro = new WhatsAppConsoleProgram();
pro.ReadJSONData(@"\TestData\package.json");
}
public void ReadJSONData(string jsonFilePath)
{
driver = new ChromeDriver(@"\SeleniumDrivers\");
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://web.whatsapp.com/");
while (true)
{
Thread.Sleep(1000);
if (isInSecondPage())
{
Thread.Sleep(15000);
this.moveToLogedInWindow();
}
}
}
private bool isInSecondPage()
{
try
{
if (driver.FindElement(By.XPath("//*[@id='window']/div[1]/div[1]/div/img")).Displayed == true)
return false;
}catch (Exception ex){ }
return true;
}
private void moveToLogedInWindow()
{
StreamReader file = File.OpenText(@"\TestData\package.json");
JsonTextReader reader = new JsonTextReader(file);
JObject o2 = (JObject)JToken.ReadFrom(reader);
IList<JToken> results = o2["data"].Children().ToList();
foreach (JToken result in results)
{
Thread.Sleep(5000);
string user = result["searchUser"].ToString();
string type = result["Advance"]["Type"].ToString();
string fileName = string.Empty;
if (result["Advance"]["FileName"] != null)
fileName = result["Advance"]["FileName"].ToString();
string caption = result["Advance"]["Caption"].ToString();
try
{
this.SendMSGToMultipleUsers(user, type, caption, @"\TestData\" + fileName);
//Need to update sent Status
}
catch (Exception ex)
{
// //Need to update failure Status
// Update the Admin
}
}
}
public void SendMSGToMultipleUsers(string searchUser, string attachType, string postCaption, string fileName)
{
try
{
IWebElement txt_Search = driver.FindElement(By.XPath(".//*[@id='side']//input[@class='input input-search']"));
txt_Search.Click();
txt_Search.SendKeys(searchUser);
Thread.Sleep(5000);
IWebElement selectUsr_Click = driver.FindElement(By.XPath(".//*[@id='pane-side']//div[@class='chat-body']"));
selectUsr_Click.Click();
Thread.Sleep(5000);
switch (attachType)
{
case "Message":
SendMessage(postCaption);
break;
case "Image":
sendFile(attachType, postCaption, fileName);
break;
case "Document":
sendFile(attachType, postCaption, fileName);
break;
}
}
catch (Exception ex)
{
throw ex;
}
}
private void sendFile(string attachType, string postCaption, string fileName)
{
IWebElement link_Attach = link_Attach = driver.FindElement(By.XPath(".//*[@id='main']//button[@class='icon icon-clip']"));
link_Attach.Click();
Thread.Sleep(3000);
IWebElement link_Gallery = driver.FindElement(By.XPath(".//*[@id='main']/header/div[3]/div/div[2]/span/div/div/ul/li[1]/button"));
if (attachType == "Document")
link_Gallery = driver.FindElement(By.XPath(".//*[@id='main']/header/div[3]/div/div[2]/span/div/div/ul/li[3]/button"));
link_Gallery.Click();
Thread.Sleep(3000);
this.FileUpload(link_Gallery, fileName);
if (attachType != "Document")
{
IWebElement txt_InputCaption = driver.FindElement(By.XPath(".//*[@id='app']//div[@class='input input-text']"));
txt_InputCaption.Click();
txt_InputCaption.SendKeys(postCaption);
Thread.Sleep(500);
IWebElement btn_ClickSend = driver.FindElement(By.XPath(".//*[@id='app']//button[@class='btn btn-round btn-l']"));
btn_ClickSend.Click();
}
else
{
IWebElement btn_ClickSend = driver.FindElement(By.XPath(".//*[@id='app']//button[@class='btn btn-round btn-l']"));
btn_ClickSend.Click();
Thread.Sleep(3000);
SendMessage(postCaption);
}
}
private void SendMessage(string message)
{
IWebElement txt_Input = driver.FindElement(By.XPath(".//*[@id='main']//div[@class='input']"));
txt_Input.Click();
txt_Input.SendKeys(message);
Thread.Sleep(3000);
IWebElement btn_Text = driver.FindElement(By.XPath(".//*[@id='main']//button[@class='icon btn-icon icon-send send-container']"));
btn_Text.Click();
Thread.Sleep(5000);
}
public void FileUpload(IWebElement controlGallery, string fileName = null)
{
System.Windows.Forms.SendKeys.SendWait(fileName);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Thread.Sleep(5000);
}
}
}