<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ahmed Ashraf on Medium]]></title>
        <description><![CDATA[Stories by Ahmed Ashraf on Medium]]></description>
        <link>https://medium.com/@ahmedash95?source=rss-ea7529349fb6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*j9VctUbnfFZvJ5z6.jpg</url>
            <title>Stories by Ahmed Ashraf on Medium</title>
            <link>https://medium.com/@ahmedash95?source=rss-ea7529349fb6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 07:27:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ahmedash95/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Golang — Twitter @ShareAsPic app using Chromedp]]></title>
            <link>https://medium.com/@ahmedash95/golang-twitter-shareaspic-app-using-chromedp-3cbc61072c89?source=rss-ea7529349fb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/3cbc61072c89</guid>
            <category><![CDATA[twitter]]></category>
            <category><![CDATA[chrome]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[headless]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Ahmed Ashraf]]></dc:creator>
            <pubDate>Wed, 06 Nov 2019 18:27:54 GMT</pubDate>
            <atom:updated>2019-11-06T18:27:54.629Z</atom:updated>
            <content:encoded><![CDATA[<h3>Golang — Twitter @ShareAsPic app using Chromedp</h3><p>This blog is a translation of its Arabic version on <a href="https://ahmedash.com/blog/article/golang-chromedp-twitter-app/">My website</a>.</p><p>As a Facebook user when I see any tech tweet on twitter I share it with my FB community. so I thought having a twitter account that takes a screenshot from a tweet can be a nice idea.</p><h3># Stack</h3><p>- <strong>Go-lang</strong>: you can use any lang as chromedp supporting several languages but I prefer Go to get a strong knowledge.</p><p>- <strong>Go-lang Twitter API</strong>: to receive tweets through `StreamAPI` and post tweets through `Twitter API`</p><p>- <strong>Chromedp</strong>: Chrome Debugging Protocol. it allows you to use the chrome browser and interact with it to simulate real user behavior with code. also it supports headless mode -no GUI- so it can run on CI/CD pipelines to do e2e tests or any automation you need like scrapping websites and other cool stuff.</p><p>- <strong>Redis</strong>: an InMemory database we will use to store processed tweets so we don’t respond to the same tweet more than once</p><h3><strong># Source Code</strong></h3><p>https://github.com/ahmedash95/shareaspic</p><h3><strong># Scenario would be like</strong>:</h3><p>- create a twitter account @shareAsPic<br>- people can mention him in a reply to any tweet with a message like @shareAsPic share this please<br>- use twitter stream API to listen for all these tweets<br>- use Chromedp to open that tweet in the browser and take a screenshot in — a -headless- mode<br>reply to the mention with the screenshot</p><h3># Application flow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KGP-vhseKMmbA8zDzCCXsA.png" /><figcaption>Application flow</figcaption></figure><p>1 — Users always post tweets.</p><p>2 — These tweets land on Twitter’s servers which it has a lot of systems. each system has it’s own mission with that tweet (notifications, timeline, streaming API, etc..)</p><p>3 — We will use the StreamAPI to listen only to replies contain `<a href="http://twitter.com/ShareAsPic">@ShareAsPic</a>` *our twitter app* name to process it</p><p>the below example shows how to prepare the stream params to listen only for our app’s mentions</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3d1498739c7c39b7786e084dbbd4795/href">https://medium.com/media/b3d1498739c7c39b7786e084dbbd4795/href</a></iframe><p>4 — At this step our app starts to receive tweets data twitter sending to us through StreamAPI</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17048c8b08138330f3dabdbe86cb2317/href">https://medium.com/media/17048c8b08138330f3dabdbe86cb2317/href</a></iframe><p>from the above `Struct` we send `InReplyToScreenName` and `InReplyToStatusID` to chromedp to open tweet URL on twitter and take the screenshot</p><p>5 — here we realize the power of headless browsers as `chromedp` simulates real user behavior on a real browser and makes interactions.<br>for us, it opens `<a href="https://twitter.com/%7BUserName%7D/status/%7BTweetID%7D%60">https://twitter.com/{UserName}/status/{TweetID}`</a> and using one of the chromedp functions takes a screenshot of that tweet using element selector that contains tweet body</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5f9lCJlA0cZCUcfdbZ48gw.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7b9f49d961a8246ad53104a5eff4437/href">https://medium.com/media/e7b9f49d961a8246ad53104a5eff4437/href</a></iframe><p>6 — then we reply to the user with the screenshot attached</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c67192aa3fe1ae998f098c8def52cd0/href">https://medium.com/media/3c67192aa3fe1ae998f098c8def52cd0/href</a></iframe><p>and the result of the whole flow is like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*Mf8xGhW60xoxDYcBbL3ZRw.png" /></figure><p># Conclusions</p><p>The app itself has no goal but it was just a simple idea to implement and see how can we automate browser interactions with Chromedp. it adds huge value for E2E tests especially for SPA. or scrapping websites content.</p><p>If you have any comments or ideas to share about chromedp. I’m more than interested to discuss and learn.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3cbc61072c89" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understand Laravel Event::fake() with Models Events]]></title>
            <link>https://medium.com/@ahmedash95/understand-laravel-event-fake-with-models-events-57a933694e83?source=rss-ea7529349fb6------2</link>
            <guid isPermaLink="false">https://medium.com/p/57a933694e83</guid>
            <category><![CDATA[unittest]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Ahmed Ashraf]]></dc:creator>
            <pubDate>Wed, 16 May 2018 13:47:10 GMT</pubDate>
            <atom:updated>2018-05-16T13:47:10.976Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*DxtYz66_qSte6QLiBwETiA.jpeg" /></figure><p>If you are a laravel developer who cares about writing unit tested software then you might be faced this problem before or at least you are going to face it soon,</p><p>In my case It was a task that allows our users to create widgets for their websites, each widget has a configuration with the total number of ads and the total number of recommended articles.</p><p>So, I have a Widget model and WidgetConfiguration model. so every time I want to create a new widget I want to create a configuration row for it so I used Laravel model events to do it.</p><pre><em>class </em>Widget <em>extends </em>Model<br>{<br>    <em>public static function </em>boot()<br>    {<br>        <em>parent</em>::boot();<br><br>        <em>static</em>::created(<em>function</em>($model){<br>            dispatch(<em>new </em>CreateWidgetConfiguration($model));<br>       });<br>     }<br>}</pre><p><strong>WidgetsController.php</strong></p><pre>public function store(WidgetStoreRequest $request){   <br>    Widget::create($request-&gt;all());<br>    dispatch(<em>new </em>NewWidgetCreated($model));<br>}</pre><p>So every time I create an widget it will fire CreateWidgetConfiguration event and there is an another event called NewWidgetCreated event that will send an emails to account managers.<br>When It comes to writing tests for it and you want now to write tests to make sure every time you create a new widget it must create a new configuration for it but you don’t want to fire a NewWidgetCreated event because we don’t want to send an email or notification every time we run tests.<br>Let’s write the unit tests first</p><pre><em>public function </em>test_create_widget_with_configuration()<br>{</pre><pre>    Event::fake();</pre><pre>    $user = factory(User::<em>class</em>)-&gt;create();<br><br>    $response = $this-&gt;actingAs($user)<br>    -&gt;json(&quot;POST&quot;,&quot;/api/widgets&quot;,[<br>        &#39;name&#39; =&gt; &#39;My First Widget&#39;,<br>        &#39;domain&#39; =&gt; &#39;https://my-first-widget.com&#39;,<br>    ]);</pre><pre>    $this-&gt;assertDatabaseHas(&#39;widgets_configuration&#39;,[<br>        &#39;widget_id&#39; =&gt; 1<br>    ]);</pre><pre>}</pre><p>the previous test will always fails because Event::fake() won’t fire the CreateWidgetConfiguration event.<br>Let’s take a look at content of fake method</p><pre><em>public static function </em>fake($eventsToFake = [])<br>{<br>    <em>static</em>::swap($fake = <em>new </em>EventFake(<em>static</em>::getFacadeRoot(), $eventsToFake));<br><br>    Model::setEventDispatcher($fake);<br>}</pre><p>so the previous block of code says that It will fake Events and replace the event dispatcher of the model to be the <strong>EventFake</strong> class.<br>So to solve this issue and only fake the global Event class without model dispatcher we have to set Model dispatcher manually after calling event fake. so the final result will be something like</p><pre>$initialEvent = Event::getFacadeRoot();<br>Event::fake();<br>Model::setEventDispatcher($initialEvent);</pre><p>and the final test case method is</p><pre><em>public function </em>test_create_widget_with_configuration()<br>{</pre><pre>    $initialEvent = Event::getFacadeRoot();<br>    Event::fake();<br>    Model::setEventDispatcher($initialEvent);</pre><pre>    $user = factory(User::<em>class</em>)-&gt;create();<br><br>    $response = $this-&gt;actingAs($user)<br>    -&gt;json(&quot;POST&quot;,&quot;/api/widgets&quot;,[<br>        &#39;name&#39; =&gt; &#39;My First Widget&#39;,<br>        &#39;domain&#39; =&gt; &#39;https://my-first-widget.com&#39;,<br>    ]);</pre><pre>$this-&gt;assertDatabaseHas(&#39;widgets_configuration&#39;,[<br>        &#39;widget_id&#39; =&gt; 1<br>    ]);</pre><pre>}</pre><p>This is it for now. Hope you at least have got the idea of how event fake works. The main purpose of the article was to let you know what it is and how to deal with it.</p><p>And if you find any kind of misinformation here, please feel free to let me know and I will update it.</p><blockquote>Happy coding…</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57a933694e83" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>