<?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 tandt53 on Medium]]></title>
        <description><![CDATA[Stories by tandt53 on Medium]]></description>
        <link>https://medium.com/@tandt53?source=rss-50bf1a8b68d3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cs76krDwGq3KYM7B</url>
            <title>Stories by tandt53 on Medium</title>
            <link>https://medium.com/@tandt53?source=rss-50bf1a8b68d3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 14:38:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tandt53/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[Check your message before Claude Code sees it]]></title>
            <link>https://tandt53.medium.com/check-your-message-before-claude-code-sees-it-f1ea55417ab5?source=rss-50bf1a8b68d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1ea55417ab5</guid>
            <category><![CDATA[claude-code]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[harness]]></category>
            <category><![CDATA[secrets]]></category>
            <dc:creator><![CDATA[tandt53]]></dc:creator>
            <pubDate>Sun, 15 Nov 2026 11:13:41 GMT</pubDate>
            <atom:updated>2026-11-15T11:19:03.994Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rDSEe9nYpKQuI-fL.png" /></figure><p>Copy a failing request from DevTools as curl, paste it into an AI coding agent and ask why it returns 401. The Authorization header goes to the model with the rest of the message. Nothing in Claude Code checks for it first.</p><p>So I added a check that does. It reads each message before it is sent and stops the ones that hold an API key.</p><h3>Why it matters</h3><p>Developers and testers paste things into AI coding agents all day: a failing curl command, a request copied from DevTools, twenty lines of a CI log, a config file that will not load. Any of those can carry an Authorization header, a session cookie or a key. Once it is in the message, it goes to the model with everything else, and a key that has left the machine has to be rotated.</p><p>I keep my Claude Code setup in an open-source repository, and this check is one of its hooks: a script Claude Code runs at a fixed point, here on every message before the model sees it.</p><h3>What did not work</h3><p><strong>A rule in the agent’s instructions.</strong> “Never handle secrets” is a rule for the agent. The key comes in with the paste, and by the time the agent reads any rule, the message has already been sent.</p><p><strong>Masking the key before it is sent.</strong> My first idea was to find the key, replace it with ghp_... and send the rest. Claude Code runs a UserPromptSubmit hook on every message before the model sees it, but that hook can only block a message or let it through. It cannot rewrite it.</p><p>So the check had to be a hook, and a hook that blocks.</p><h3>What worked: block only what is certainly a secret</h3><p>A guard that blocks on a guess gets bypassed by habit. If every third message is stopped, people start typing the word that sends a message anyway, without reading the warning. So the hook has two levels:</p><ul><li><strong>Block:</strong> forms that are certainly secrets. GitHub tokens (ghp_, github_pat_ and the other kinds), Anthropic keys, AWS access key ids, private key blocks, JWTs, and a credential header or session cookie whose value looks real (more on that below).</li><li><strong>Warn only:</strong> anything else that looks like a secret. When gitleaks, an open-source secret scanner, is installed, the message also goes through gitleaks stdin with a 2-second budget; on my machine it answers in about 20 ms. A gitleaks rule for one of the certain forms blocks. A generic rule such as generic-api-key only warns.</li></ul><p>When it blocks, it says where and what, never the value. Here it stops a curl command in Claude Code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gtDsrAwoXqM11GjRfeNGFQ.png" /></figure><p><em>The token in the prompt is a fake one, covered here anyway. The hook’s own message shows only </em><em>ghp_ct...5VYy.</em></p><p>The value is masked so the warning does not become one more place the key is written.</p><h3>Headers: check the name or the value?</h3><p>This was the real design question, because a pasted request is mostly headers.</p><ul><li>Check only the header <strong>name</strong>, and every doc that says Authorization: Bearer $TOKEN is blocked.</li><li>Check only the <strong>value</strong>, and every commit hash and UUID looks like a key.</li></ul><p>So the hook needs both: a credential header (Authorization, X-API-Key, X-Auth-Token and a few more) or a session cookie, <em>and</em> a value that looks real. “Looks real” is deliberately plain (comments added here):</p><pre>def looks_real(value: str) -&gt; bool:<br>    &quot;&quot;&quot;A header value that is a credential, not a placeholder or a word.&quot;&quot;&quot;<br>    value = value.strip()<br>    # short, a placeholder ($TOKEN, &lt;your-key&gt;, xxxx...), or too few distinct characters<br>    if len(value) &lt; 16 or PLACEHOLDER.search(value) or len(set(value)) &lt; 6:<br>        return False<br>    # a real credential mixes letters and digits<br>    return bool(re.search(r&quot;[A-Za-z]&quot;, value) and re.search(r&quot;\d&quot;, value))</pre><p>$TOKEN, &lt;your-key&gt;, xxxxxxxxxxxxxxxx and process.env.API_KEY pass as placeholders. A Basic value is decoded first and blocks only when it holds user:password.</p><p>Two ways out exist on purpose. [secret-ok] at the start of a message sends it anyway, with a warning, for the day the &quot;secret&quot; is a test fixture the agent needs to see. CLAUDE_SKIP_SECRET_GUARD=1 turns the hook off. And if the hook itself crashes, the message goes through with a &quot;could not scan&quot; warning: a broken guard must never trap a message.</p><h3>The result, and what it still cannot do</h3><p>Now a message like the one at the top stops before it is sent. In a test with a fake GitHub token, the block took zero model turns, so nothing was spent.</p><p>The same test showed what a block does not do. The blocked message is not gone: its full text was still written once to the session transcript on disk. The guard keeps a secret from the model, not off the machine. A key that was pasted still needs rotating.</p><p>How I know the hook works:</p><ul><li><strong>18 tests</strong>, with fake keys built at run time, so the repository holds nothing a scanner would take for a key.</li><li><strong>18 deliberate bugs.</strong> I broke a copy of the hook 18 ways and checked that a test failed each time. One break survived at first: removing the distinct-character rule failed no test, because the only dummy value in the tests, xxxx..., was already caught as a placeholder. A case with ab12ab12ab12ab12ab12 now covers it.</li></ul><h3>What I’d tell any team using AI coding agents</h3><ol><li>Guard the message, not the agent: the key comes in with the paste.</li><li>Block only certainties and warn on guesses, or the way out becomes muscle memory.</li><li>For headers, judge the name and the value together.</li><li>Know what a block does not do. The text can still sit on disk, so rotate a pasted key anyway.</li></ol><p>The hook is one Python file, standard library only: <a href="https://github.com/tandt53/claude-code-setup/blob/main/hooks/secret_guard.py">hooks/secret_guard.py</a> in my open-source Claude Code setup (Apache 2.0). <a href="https://github.com/tandt53/claude-code-setup/blob/main/docs/MECHANISMS.md">docs/MECHANISMS.md</a> lists what it enforces, what it does not, and how it was tested.</p><p><em>Originally published at </em><a href="https://tandt53.com/check-your-message-before-claude-code-sees-it"><em>https://tandt53.com</em></a><em> on November 15, 2026.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1ea55417ab5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simplifying getText in Flutter Driver: A Better Approach]]></title>
            <link>https://tandt53.medium.com/simplifying-gettext-in-flutter-driver-a-better-approach-48b0bc0d4087?source=rss-50bf1a8b68d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/48b0bc0d4087</guid>
            <category><![CDATA[mobile-automation-testing]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[automation-testing]]></category>
            <dc:creator><![CDATA[tandt53]]></dc:creator>
            <pubDate>Sat, 07 Dec 2024 09:20:39 GMT</pubDate>
            <atom:updated>2024-12-07T09:20:39.514Z</atom:updated>
            <content:encoded><![CDATA[<p>In my previous <a href="https://medium.com/@tandt53/solving-gettext-issues-with-appium-flutter-driver-a-complete-guide-1b2834da5d32">post</a>, I shared a basic solution for getText functionality with flutter:dragAndDropWithCommandExtension in appium-flutter-driver. Today, I’ll present a simpler and more robust solution that addresses two key limitations of the original approach:</p><ol><li>Lack of support for ancestor and descendant selectors</li><li>Complex implementation requiring custom finder handling</li></ol><p>The solution was accepted and released in appium-flutter-driver v2.11.0</p><h3>A New Solution: Using Built-in Flutter Driver Finders</h3><p>Instead of creating a custom finder system, we can leverage appium-flutter-driverbuilt-in finder methods combined with a custom Command Extension. This approach simplifies both the implementation and usage.</p><h3>Implementation</h3><p>Firstly, please update the driver to the latest version &quot;appium-flutter-driver&quot;: &quot;2.11.0&quot;</p><h4>Appium Script</h4><p>The test script becomes remarkably simple:</p><pre>import {byValueKey} from &quot;appium-flutter-finder&quot;;<br>const payload = {<br>    &#39;findBy&#39;: byValueKey(&#39;amount&#39;),<br>};<br>const getTextResult = await driver.execute(&#39;flutter:getTextWithCommandExtension&#39;, payload);</pre><h3>Flutter Implementation</h3><ol><li>Add the command extension code to your project</li></ol><ul><li>Copy the implementation from the <a href="https://github.com/appium/appium-flutter-driver/blob/main/example/dart/get_text_command.dart">example repository</a></li><li>Place it in your lib folder.</li></ul><p>2. Register the command in your main.dart:</p><pre>import &#39;drag_commands.dart&#39;;<br>import &#39;get_text_command.dart&#39;;<br>void main() {<br>  enableFlutterDriverExtension(<br>      commands: [DragCommandExtension(), GetTextCommandExtension()]);<br>  runApp(const MyApp());<br>}</pre><p>3. Register the command in your main.dart</p><pre>import &#39;drag_commands.dart&#39;;<br>import &#39;get_text_command.dart&#39;;<br>void main() {<br>  enableFlutterDriverExtension(<br>      commands: [DragCommandExtension(), GetTextCommandExtension()]);<br>  runApp(const MyApp());<br>}</pre><p>4. Build and run your project That’s it!</p><h4>How It Works</h4><ol><li>Locator Creation:</li></ol><ul><li>byValueKey (or any other finder method) generates a base64-encoded locator string.</li><li>This string contains all necessary information to find the widget.</li></ul><p>2. Command Execution:</p><ul><li>The locator is sent to Flutter Driver via getTextWithCommandExtension</li><li>Flutter Driver forwards the base64 string to your flutter application.</li></ul><p>3. Widget Location and Text Extraction:</p><ul><li>The Flutter app decodes the base64 string into a SerializableFinder.</li><li>Locates the widget in the widget tree.</li><li>Extracts text content using appropriate methods based on widget type.</li></ul><h3>Conclusion</h3><p>This solution provides a more straightforward approach to getText functionality while maintaining reliability and extensibility. By using Flutter Driver’s built-in finder methods, we reduce complexity and potential points of failure. Please always verify that widgets are visible before attempting to extract text.</p><h3>References</h3><ul><li><a href="https://github.com/appium/appium-flutter-driver?tab=readme-ov-file#command-extension-flutter-driver">Using command extension in appium-flutter-driver</a></li><li><a href="https://github.com/appium/appium-flutter-driver/pull/755">My PR contributed in appium-flutter-driver</a></li><li><a href="https://github.com/Alpaca00/command-driven-list">Example flutter project</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48b0bc0d4087" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solving getText Issues with Appium Flutter Driver: A Complete Guide]]></title>
            <link>https://tandt53.medium.com/solving-gettext-issues-with-appium-flutter-driver-a-complete-guide-1b2834da5d32?source=rss-50bf1a8b68d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b2834da5d32</guid>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[gettext]]></category>
            <category><![CDATA[appium]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[appium-flutter-driver]]></category>
            <dc:creator><![CDATA[tandt53]]></dc:creator>
            <pubDate>Sat, 30 Nov 2024 15:56:07 GMT</pubDate>
            <atom:updated>2024-12-07T09:22:18.863Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, I encountered and solved an issue with getting text from Flutter widgets using Appium Flutter Driver. This guide will help others facing similar challenges.</p><h3>The Problem</h3><p>When using Appium’s Flutter Driver, attempting to get text from widgets using driver.getElementText(selector) can result in unexpected errors, especially when dealing with Text widgets. This issue commonly occurs with TextSpan widgets.</p><p>The error looks like this:</p><pre>unknown error: An unknown server-side error occurred while processing the command. Original error: Cannot execute command get_text, server response {<br>  &quot;isError&quot;: true,<br>  &quot;response&quot;: &quot;Uncaught extension error while executing get_text: Unsupported operation: Type Text is currently not supported by getText\n#0      CommandHandlerFactory._getText (package:flutter_driver/src/common/handler_factory.dart:409:7)\n&lt;asynchronous suspension&gt;\n#1      FlutterDriverExtension.call (package:flutter_driver/src/extension/extension.dart:370:31)\n&lt;asynchronous suspension&gt;\n#2      BindingBase.registerServiceExtension.&lt;anonymous closure&gt; (package:flutter/src/foundation/binding.dart:950:18)\n&lt;asynchronous suspension&gt;\n#3      _runExtension.&lt;anonymous closure&gt; (dart:developer-patch/developer.dart:112:11)\n&lt;asynchronous suspension&gt;\n&quot;,<br>  &quot;type&quot;: &quot;_extensionType&quot;,<br>  &quot;method&quot;: &quot;ext.flutter.driver&quot;<br>}</pre><p>I tried to switch to NATIVE_APP context and get text, but it returned empty string.</p><h3>Understanding the Cause</h3><p>The issue occurs specifically with Text widgets that use TextSpan. Here’s an example of such a widget:</p><pre>Text.rich(<br>    TextSpan(children: [<br>       TextSpan(text: controll.getAmount()),<br>       TextSpan(text: controll.getCurrency()),<br>    ],<br>   ),<br>    key: const Key(&#39;amount&#39;),<br>),</pre><p>or you can refer to a similar <a href="https://github.com/appium/appium-flutter-driver/issues/585">issue</a>.</p><h3>The Solution</h3><p><em>Note: I created a new post with better solution </em><a href="https://tandt53.medium.com/simplifying-gettext-in-flutter-driver-a-better-approach-48b0bc0d4087"><em>here</em></a><em>. You can check it out.</em></p><p>After extensive research and experimentation, I developed a solution using Flutter’s driver extension capabilities. Here’s a step-by-step guide to implementing it.</p><h3>Prerequisite</h3><p>Before implementing the solution, ensure you have:</p><ul><li>Add `“appium-flutter-driver”: “2.10.0”` into package.json (npm install appium-flutter-driver@2.10.0)</li></ul><h3>Step 1: send locator from appium script to flutter driver</h3><p>I found an example of how to send locator from appium script to flutter driver in <a href="https://github.com/Alpaca00/command-driven-list">this</a> repository. We can use the flutter:dragAndDropWithCommandExtension command with some parameters (startX, startY, endX, endY, Duration) to drag &amp; drop an element on the screen. Then I decided to use startX as locator find by, and startY as value key.</p><pre>const payload = {<br>    &quot;startX&quot;: &quot;value_key&quot;,<br>    &quot;startY&quot;: &#39;amount&#39;,<br>    &quot;endX&quot;: &quot;1&quot;,<br>    &quot;endY&quot;: &quot;1&quot;,<br>    &quot;duration&quot;: &quot;5000&quot;<br>}<br>const result = await driver.execute(&quot;flutter:dragAndDropWithCommandExtension&quot;, payload);<br>console.log(JSON.stringify(result));</pre><h3>Step 2: Revise the flutter code of <a href="https://github.com/Alpaca00/command-driven-list">sample project</a></h3><p>There are some steps to follow to get the correct locator.</p><ul><li>Change final double startX to final String startX in DragCommand class</li><li>Change final double startY to final String startY in DragCommand class</li><li>Change DragCommandExtension.call() method to get the widget element and get text data from Text widget</li><li>Return the text data DragResult(true, additionalData: {&#39;text&#39;: textData});</li></ul><p>Here is the sample code.</p><pre>class DragCommand extends Command {<br>  final String startX;<br>  final String startY;<br>  final double endX;<br>  final double endY;<br>  final Duration duration;<br><br>  DragCommand(<br>      this.startX, this.startY, this.endX, this.endY, this.duration);<br><br>  @override<br>  String get kind =&gt; &#39;dragAndDropWithCommandExtension&#39;;<br><br>  DragCommand.deserialize(Map&lt;String, String&gt; params)<br>      : startX = params[&#39;startX&#39;]!,<br>        startY = params[&#39;startY&#39;]!,<br>        endX = double.parse(params[&#39;endX&#39;]!),<br>        endY = double.parse(params[&#39;endY&#39;]!),<br>        duration = Duration(milliseconds: int.parse(params[&#39;duration&#39;]!));<br>}<br><br>class DragCommandExtension extends CommandExtension {<br>  String getTextFromWidget(Widget widget) {<br>    if (widget is Text) {<br>      // If direct text data is available, return it<br>      if (widget.data != null) {<br>        return widget.data!;<br>      }<br>      // Otherwise get text from textSpan<br>      if (widget.textSpan != null) {<br>        return getTextFromTextSpan(widget.textSpan!);<br>      }<br>    }<br>    return &#39;getTextFromWidget&#39;;<br>  }<br><br>  String getTextFromTextSpan(InlineSpan span) {<br>    if (span is TextSpan) {<br>      final List&lt;String&gt; textParts = &lt;String&gt;[];<br><br>      // Add current text if not null<br>      if (span.text != null) {<br>        textParts.add(span.text!);<br>      }<br><br>      // Add children&#39;s text recursively<br>      if (span.children != null) {<br>        for (final InlineSpan child in span.children!) {<br>          textParts.add(getTextFromTextSpan(child));<br>        }<br>      }<br><br>      return textParts.join(&#39;,&#39;);<br>    }<br>    return &#39;getTextFromTextSpan&#39;;<br>  }<br><br>  @override<br>  Future&lt;Result&gt; call(<br>      Command command,<br>      WidgetController prober,<br>      CreateFinderFactory finderFactory,<br>      CommandHandlerFactory handlerFactory) async {<br>    final DragCommand dragCommand = command as DragCommand;<br><br>    // Create finder for Text widget<br>    final type = dragCommand.startX;<br>    final value = dragCommand.startY;<br><br>    SerializableFinder serializableFinder;<br>    switch(type){<br>      case &#39;type&#39;:<br>        serializableFinder = ByType(value);<br>        break;<br>      case &#39;text&#39;:<br>        serializableFinder = ByText(value);<br>        break;<br>      case &#39;value_key&#39;:<br>        serializableFinder = ByValueKey(value);<br>        break;<br>      // ancestor and descendant are not supported yet, we will improve it later<br>      default:<br>        throw Exception(&quot;Type &#39;$type&#39; is not supported&quot;);<br>    }<br><br>    // Get the widget element<br>    final Element element = prober.element(finder);<br><br>    // Get text data from Text widget<br>    String textData = &#39;&#39;;<br><br>    if (element.widget is Text) {<br>      textData = getTextFromWidget(element.widget);<br>    } else {<br>      textData = &#39;Not a Text widget&#39;;<br>    }<br><br>    return DragResult(true, data: {&#39;text&#39;: textData});<br>  }<br><br>  @override<br>  String get commandKind =&gt; &#39;dragAndDropWithCommandExtension&#39;;<br><br>  @override<br>  Command deserialize(<br>      Map&lt;String, String&gt; params,<br>      DeserializeFinderFactory finderFactory,<br>      DeserializeCommandFactory commandFactory) {<br>    return DragCommand.deserialize(params);<br>  }<br>}</pre><p>When successful, you’ll see output similar to this:</p><pre>[0-0] 2024-11-28T08:42:38.681Z INFO webdriver: [POST] http://localhost:4723/session/ffe9d089-e722-4926-be64-ca791eae3dcb/execute/sync<br>[0-0] 2024-11-28T08:42:38.682Z INFO webdriver: DATA {<br>[0-0]   script: &#39;flutter:dragAndDropWithCommandExtension&#39;,<br>[0-0]   args: [<br>[0-0]     {<br>[0-0]       startX: &#39;value_key&#39;,<br>[0-0]       startY: &#39;amount&#39;,<br>[0-0]       endX: &#39;1&#39;,<br>[0-0]       endY: &#39;1&#39;,<br>[0-0]       duration: &#39;5000&#39;<br>[0-0]     }<br>[0-0]   ]<br>[0-0] }<br>[0-0] 2024-11-28T08:42:38.758Z INFO webdriver: RESULT {<br>[0-0]   isError: false,<br>[0-0]   response: { success: true, text: &#39;USD 10.00&#39; },<br>[0-0]   type: &#39;_extensionType&#39;,<br>[0-0]   method: &#39;ext.flutter.driver&#39;<br>[0-0] }</pre><h3>How It Works</h3><p>The solution works by:</p><ul><li>Creating a custom Flutter driver extension that can access the widget tree</li><li>Using the extension to extract text from both simple Text widgets and complex TextSpan structures</li><li>Leveraging the existing dragAndDropWithCommandExtension command to communicate between Appium and Flutter</li></ul><p>Benefits</p><ul><li>Works with both simple Text widgets and complex RichText structures</li><li>More reliable than alternative approaches like switching to NATIVE_APP context</li><li>Can be extended to support additional widget types</li><li>Provides consistent results across different Flutter versions</li></ul><h3>References</h3><ul><li><a href="https://github.com/appium/appium-flutter-driver/issues/585">GitHub Issue Discussion</a></li><li><a href="https://github.com/Alpaca00/command-driven-list">Flutter Driver Extension Examples</a></li><li><a href="https://github.com/appium/appium-flutter-driver">Appium Flutter Driver Documentation</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b2834da5d32" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Is API testing black box or white box?]]></title>
            <link>https://tandt53.medium.com/is-api-testing-black-box-or-white-box-a49903f5bd64?source=rss-50bf1a8b68d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/a49903f5bd64</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[api-testing]]></category>
            <category><![CDATA[white-box-testing]]></category>
            <category><![CDATA[black-box-testing]]></category>
            <dc:creator><![CDATA[tandt53]]></dc:creator>
            <pubDate>Mon, 13 Feb 2023 03:46:11 GMT</pubDate>
            <atom:updated>2023-02-13T03:46:11.684Z</atom:updated>
            <content:encoded><![CDATA[<p>This is the question you might be asked during an interview for QA position. And you may have an answer and a reason for that answer. In this post, I will provide my response to this question and explain my reasoning.</p><p>Please note that the term API mentioned in this post refers to web services like REST, SOAP.</p><h3>Black and white box testing definition</h3><p>I will discuss the definition of 2 testing types. According to the Guru99 post, <a href="https://www.guru99.com/back-box-vs-white-box-testing.html">Difference Between Black Box and White Box Testing</a> mentioned:</p><blockquote><strong><em>Black Box testing</em></strong><em>: It is a testing approach which is used to test the software without the knowledge of the internal structure of program or application.</em></blockquote><blockquote><strong><em>White Box testing</em></strong><em>: It is a testing approach in which the internal structure is known to the tester.</em></blockquote><p>I would want to rephrase the above definitions a bit: <em>if you can see the internal structure or implementation of the test target, that means it’s a white box, otherwise a black box.</em></p><h3>Debate</h3><p>If you ask this question to your colleagues, you might get some different answers like these:</p><ul><li><strong>Black box</strong>: When testing APIs, testers typically use Postman or a framework to send requests, then check the response. That’s obviously a black box because testers do not care about how APIs are created.</li><li><strong>White box</strong>: As a counterpoint, sometimes testers can see the source code of APIs as well as understand the flow of information through that code. So it should be a white box.</li><li><strong>Grey box/gray box</strong>: Another example would be that testers are verifying functions on UI (Web or Mobile) and they can know exactly what APIs are called under that UI. So, it can be a grey box.</li></ul><h3>My answer</h3><p>I’d like to discuss some factors to support my answer:</p><ul><li><strong>Test target</strong> (or test object): API testing means targets are APIs, not UI. In case that testers can use API to support UI testing, but API is not the target of testing activities, it is UI. Therefore, the gray box with the given explanation is not correct.</li><li><strong>Black and white box are two sets of techniques</strong> used during tests. Black box techniques include <em>Equivalence partition</em>, <em>Boundary Value</em>, <em>Decision</em> <em>Table</em>, etc. White box techniques include <em>Statement Coverage</em>, Branch <em>Coverage</em>, etc.</li></ul><p>With the above discussion, the question seems to try to match a testing type to a testing technique, which is not appropriate. When asked this concern, we (QA/testers) tend to mention that API testing involves sending APIs and checking the response by using Postman or any similar tool/framework. This is a black box, because we’ll use some black box techniques to test it. In contrast, it could be white box, if we apply coverage techniques to verify APIs.</p><h3>Summary</h3><p>In this post, I have presented my answers to the question in the title. The choice of black box or white box depends on which techniques will be used to ensure the API quality.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a49903f5bd64" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Do not place assertions in Page Objects, why?]]></title>
            <link>https://tandt53.medium.com/why-should-automation-test-avoid-putting-verify-commands-in-page-object-a10b0d455864?source=rss-50bf1a8b68d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/a10b0d455864</guid>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[assertions]]></category>
            <category><![CDATA[page-object-model]]></category>
            <category><![CDATA[verification]]></category>
            <dc:creator><![CDATA[tandt53]]></dc:creator>
            <pubDate>Sun, 05 Feb 2023 09:40:52 GMT</pubDate>
            <atom:updated>2023-02-08T07:46:14.322Z</atom:updated>
            <content:encoded><![CDATA[<p>If you search for best practices for page object model in automation tests, you will find that many people suggest not using verify in Page Object (PO) methods. However, there are no any details&#39; explanation to the reasons. Even the post on <a href="https://martinfowler.com/bliki/PageObject.html">https://martinfowler.com</a> doesn’t clarify the issue.</p><blockquote>I favor having no assertions in page objects. I think you can avoid duplication by providing assertion libraries for common assertions — which can also make it easier to provide good diagnostics. [3]</blockquote><blockquote>3: One form of assertions is fine even for people like me who generally favor a no-assertion style. These assertions are those that check the invariants of a page or the application at this point, rather than specific things that a test is probing.</blockquote><p>My goal with this post is to clarify the reasons behind this suggestion based on my experience and to help you to understand it more clearly.</p><h3>Bullet No. 1</h3><p>We should define a specific <strong><em>responsibility</em></strong> for each layer in our automation project architecture. According to my last post, assertion commands are normally placed in the test layer. However, they are also placed in the page object layer. Therefore, there is a mixture of verification functions in 2 layers. That situation should be avoided.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*lzKvuZS6P6N4oKmdXeh-tQ.png" /><figcaption>Test layer and Business Layer</figcaption></figure><h3>Bullet No. 2</h3><p>You can consider methods implemented at Page Objects like APIs that communicate between client (test scenario) and server (System Under Test — SUT). These methods are usually carrying the requests (and parameters) from test layer, to automatically interact with SUT. Then they can get values from SUT and return to test layer for verification purpose. Obviously, you have never seen any APIs contains verification, don’t you?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mq_ABWLIlP9-ShrmvCJPjg.png" /><figcaption>Page object methods like APIs</figcaption></figure><h3>Bullet No. 3</h3><p>The last reason is also a complicated case that you might encounter if you put assertions at the PO level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4x5m9w-o4zqbr4U2k80ceQ.png" /><figcaption>Problem</figcaption></figure><p>Since PO methods are reusable, let’s consider the case where 2 tests use the same PO method, but that method contains verification commands for only 1 test. If that verification is failed, what will happen to these test cases? Both are failed, but:</p><ul><li>Test01 is fine because the test purpose is covered.</li><li>For Test02, it’s not fine because its checkpoints are not checked. Main checkpoints may come after another PO method is called. Therefore, the checkpoints for Test02 are not covered, and we will waste time checking whether the failure is related to Test02.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*paGbsJptQPZmMcCblAchvw.png" /><figcaption>Solution</figcaption></figure><p>To resolve Test02 problem, we should put assertion commands in proper test cases. As the result, we will have below advantages:</p><ul><li><strong>Tests are atomic</strong>: each test has its own checkpoints</li><li>The <strong>checkpoints are all visible at the test level</strong> so that a reviewer can see the whole test without digging into the lower layers.</li><li><strong>Reduce execution time</strong>: if the verification needs to get data from the SUT, time will vary based on the number of times the PO method is used, and the connection between the test and cloud services.</li></ul><h3>Summary</h3><p>With the 3 bullets above, hopefully now you have a much better understanding of why assertion commands should be placed outside of Page Objects. In addition, it’s vital to remember that separated responsibility applies not just to methods, but also to layers in architecture.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a10b0d455864" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stop init WebDriver instance before test]]></title>
            <link>https://tandt53.medium.com/stop-init-driver-instance-before-test-e24dfd4ef43b?source=rss-50bf1a8b68d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/e24dfd4ef43b</guid>
            <category><![CDATA[architecture-pattern]]></category>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[selenium-webdriver]]></category>
            <dc:creator><![CDATA[tandt53]]></dc:creator>
            <pubDate>Tue, 08 Nov 2022 14:55:28 GMT</pubDate>
            <atom:updated>2022-11-10T06:25:35.783Z</atom:updated>
            <content:encoded><![CDATA[<p>A lot of documents and posts have been published on the internet for web automation using Selenium Webdriver that are showing WebDriver driver = new ChromeDriver() in beforeTest annotated methods. This concept will remain with you months or years later, not just me. As a result of studying <a href="https://medium.com/p/13a9e70c7158">test automation architecture</a>, I have changed my mind. In this post, I will help you correct a wrong concept.</p><pre>public class LoginTest {<br>    private WebDriver driver;<br>    private LoginPage loginPage;<br>    private HomePage homePage;<br><br>    @BeforeTest<br>    public void setup(){<br>        driver = new ChromeDriver();<br>        loginPage = new LoginPage(driver);<br>        homePage = new HomePage(driver);<br>    }<br>}<br><br>class LoginPage {<br>    public LoginPage(WebDriver driver) {<br>        PageFactory.initElements(driver, this);<br>    }<br>}<br><br>class HomePage {<br>    public HomePage(WebDriver driver) {<br>        PageFactory.initElements(driver, this);<br>    }<br>}</pre><h3>Remind</h3><p>Remind you to ask yourself 3 questions from <a href="https://medium.com/p/13a9e70c7158">last post</a> to define which layer should driver initialization be placed.</p><ul><li>Responsibility - <em>what is it for?</em> For testing, business, automating or support.</li><li>Dependencies - <em>what are its dependencies?</em> Test layer has 2 dependencies: the business and core layers. But the business layer depends on the core.</li><li>Dependents - <em>what are its dependents?</em> Test layer does not have any dependents, business layer has 1 dependent (test layer), core layer has 2 dependents (business and test layers).</li></ul><h3>Answers</h3><ul><li>Responsibility - what Webdriver instance is for? to find and interact with elements shortly. It&#39;s nothing with test.</li><li>Dependencies - what are its dependencies? driver&#39;s dependencies are capabilities, not the test itself.</li><li>Dependents - what are its dependents? Page need driver instance to factory elements, tests do no use it directly.</li></ul><p>As the above answers demonstrate, the driver instance is useless at test level. In the before-test method, you create and pass it to the page object’s constructor. Why do we have to put it in the test? It also causes high coupling between test objects and page objects, so it’s not a wise idea.</p><h3>Solution</h3><p>Now it’s clear to not start the driver at test level, right? We should do that at the page object level. However it’s often needed whenever a constructor is invoked, so we will create a driver instance inside constructors.</p><pre>public class LoginTest(){<br>    private LoginPage loginPage;<br>    private HomePage homePage;<br>    public void beforeTest(){<br>        loginPage = new LoginPage();<br>        homePage = new HomePage();<br>    }<br>}</pre><pre>class LoginPage {<br>    public LoginPage() {<br>        WebDriver driver = new ChromeDriver();<br>        PageFactory.initElements(driver, this);<br>    }<br>}<br><br>class HomePage {<br>    public HomePage() {<br>        WebDriver driver = new ChromeDriver();<br>        PageFactory.initElements(driver, this);<br>    }<br>}</pre><p>Now that you see the driver has disappeared from the LoginTest class, that means the dependency has been removed. However, the page objects&#39; constructors duplicate driver initialization. So it should separate it into another class, like a driver manager.</p><pre>class DriverManager {<br>    private static WebDriver driver;<br><br>    public static WebDriver getDriver() {<br>        if (driver == null) {<br>            driver = new ChromeDriver();<br>        }<br>        return driver;<br>    }<br>}</pre><pre>class LoginPage {<br>    public LoginPage() {<br>        WebDriver driver = DriverManager.getDriver();<br>        PageFactory.initElements(driver, this);<br>    }<br>}<br><br>class HomePage {<br>    public HomePage() {<br>        WebDriver driver = DriverManager.getDriver();<br>        PageFactory.initElements(driver, this);<br>    }<br>}</pre><p>DriverManager.getManager() handles code that prevents driver initialization multiple times.</p><h3>Summary</h3><p>In this post, we discussed why we should not create driver instances at the test level. No matter what browser is running, it helps keep tests clean and limited to business features. Furthermore, the coupling between test and WebDriver is lost.</p><p>The code presented in this post is a simple solution to remove test dependencies. In my next post, I will describe a more advanced technique that uses dependency injection.</p><p><em>Original post: </em><a href="https://tandt53.com/stop-init-driver-instance-before-test"><em>https://tandt53.com/stop-init-driver-instance-before-test</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e24dfd4ef43b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Test Automation Architecture]]></title>
            <link>https://tandt53.medium.com/test-automation-architecture-13a9e70c7158?source=rss-50bf1a8b68d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/13a9e70c7158</guid>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[architecture]]></category>
            <dc:creator><![CDATA[tandt53]]></dc:creator>
            <pubDate>Fri, 04 Nov 2022 15:40:07 GMT</pubDate>
            <atom:updated>2022-11-04T15:40:07.564Z</atom:updated>
            <content:encoded><![CDATA[<h3>Overview the architecture for automation test</h3><p>As an automation test engineer, you might have wondered what a good scenario is and how well our automation tests are organized. Or just asking yourself where should the line/method/module be placed? This post will provide you with a clear understanding of the architecture of automation test.</p><h3>Layered architecture</h3><p>Layered architecture is a well-known pattern used in many enterprise software solutions. Components in this architecture are horizontally layered, with each component responsible for its own feature. Normally it has 3 or 4 layers as <a href="https://www.baeldung.com/cs/layered-architecture">Baeldung post</a> mentions:</p><ul><li><strong>Presentation layer</strong>: responsible for user interactions with the software system</li><li><strong>Business layer</strong>: handles aspects related to accomplishing functional requirements</li><li><strong>Persistence layer</strong>: responsible for algorithms, and programming components</li><li><strong>Database layer</strong>: responsible for handling data, databases</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tDD0uQXx3QVtgU_W" /><figcaption>source: Mark Richard’s book “Software architecture patterns.”</figcaption></figure><p>This post does not go into detail about this pattern, because there are many documents on the internet you can find and read. Test automation is like a software solution/product, so it can definitely follow this kind of pattern to inherit all its advantages.</p><h3>Test automation layers</h3><p>I will list down some basic layers in test automation, with code samples, and how to define which layer the line/method of code belongs to</p><p>Here is a basic example of web automation using Selenium Webdriver:</p><pre>public class LoginTest{<br>    <a href="http://twitter.com/Test">@Test</a><br>    public void loginSuccessfully(){<br>        loginPage.login(username, password);<br>        Assert.assertTrue(homePage.isDisplayed());<br>    }<br>}<br>public class LoginPage(){<br>    public void login(String username, String password){<br>        this.txtUsername.sendKeys(username);<br>        this.txtPassword.sendKeys(password);<br>        this.btnLogin.click();<br>    }<br>}</pre><ul><li><strong>Test layer</strong> simply is the set of all test classes, and methods that arrange scenarios, and checkpoint verifications into test cases and suites. During test execution, this layer will call methods from the business layer (loginPage.login()) and from the core layer (Assert.assertTrue()). In general, a test method represents a set of scenario steps that include use cases and expected results.</li><li><strong>Business layer</strong> encapsulates all the business features of an application and converts them into programmable methods with parameters (if needed). These methods describe user steps in the user scenario, not the test scenario, so assert commands should never appear at this level. Normally <strong>page objects</strong> are considered part of the business layer.</li><li><strong>Core layer</strong> contains all the methods available in libraries or frameworks to execute tests on an application. These methods can come from:<br>- Test libraries/frameworks: Assert.assertTrue(), Assert.assertEquals()<br>- Web automation: sendKeys(), click(), etc. from Selenium WebDriver<br>- API automation: given(), when(), then() from Rest Assured.<br>- Etc.</li></ul><h3>Layers Principles</h3><p>As well as layer responsibilities, there is a rule for placing layers at the top, middle, or bottom of an architecture. The key is <strong>dependency</strong>. To choose the right position, you should answer some basic questions:</p><ul><li>Responsibility - <em>what is it for?</em> For testing, business, automating, or support.</li><li>Dependencies - <em>what are its dependencies?</em> Test layer has 2 dependencies: the business and core layers. But the business layer depends on the core.</li><li>Dependents - <em>what are its dependents?</em> Test layer does not have any dependents, business layer has 1 dependent (test layer), core layer has 2 dependents (business and test layers).</li></ul><h3>Summary</h3><p>This post summarizes the 3 basic layers of the architecture for test automation. It can serve as a helpful and fundamental outline for structuring test automation code. It is not easy to categorize everything that falls under one of the 3 layers in an automation project. However, just remember the keyword <strong>DEPENDENCY</strong> and you can freely create a new layer(s) as you want.</p><p><em>Original post: </em><a href="https://tandt53.com/test-automation-architecture"><em>https://tandt53.com/test-automation-architecture</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13a9e70c7158" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[State management in automation test scenario]]></title>
            <link>https://tandt53.medium.com/state-management-in-automation-test-scenario-1b0ac27924ac?source=rss-50bf1a8b68d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b0ac27924ac</guid>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[state-management]]></category>
            <dc:creator><![CDATA[tandt53]]></dc:creator>
            <pubDate>Fri, 04 Nov 2022 15:29:28 GMT</pubDate>
            <atom:updated>2022-11-04T15:29:28.289Z</atom:updated>
            <content:encoded><![CDATA[<h3>Along with well-organized code in a scenario, it is necessary to manage values, especially in End-2-End scenarios. Thus, this post will suggest a flow of those values inside an automation test case.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mLt9dyNAN4tSz1Fo" /><figcaption>state management</figcaption></figure><p>There are 2 main levels:</p><ul><li><strong>Test level</strong> includes all steps related to behavior and verification. The values will be referred to as entities or DAOs.</li><li><strong>Page Objects level</strong>, as defined in <a href="https://martinfowler.com/bliki/PageObject.html">Martin Fowler’s post</a>, includes methods inside a page or fragment/layout of a page.</li></ul><h3>State</h3><p>The term state is related to the status of an application during execution. It can be represented by individual values or combined properties to form object(s). In a test case, it is usually created, transferred, updated for business actions, and checked for verification.</p><h4>Example 01</h4><p>Let’s take a simple example:</p><pre><a href="http://twitter.com/Test">@Test</a><br>public void happyCase(){<br>// Arrange <br>  System.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;chromedriver.exe&quot;);<br>  WebDriver driver = new ChromeDriver();<br>  CalculationPage page = new CalculationPage(driver);<br>  int result;<br>  int a = 4;<br>  int b = 6;<br>  int expected = 10;</pre><pre>// Actions<br>  page.sum(a, b);<br>  result = page.getResult();</pre><pre>// Assert <br>  Assert.assertEquals(result, expected)<br>}</pre><p>As can be seen from above code, the variables a, b and result are considered as states.</p><ul><li>a and b are created and initialized in <em>Arrange</em> stage.</li><li>Then these variables are passed to the sum() method of CalculationPage</li><li>After method consumes variables, it will return result.</li><li>The returned value is compared with the expected value.</li></ul><h4>Example 02</h4><p>Assume that we have to design a test case to test the payment flow. So let’s create some classes below:</p><pre>// using lombok annotation<br><a href="http://twitter.com/Setter">@Setter</a><br><a href="http://twitter.com/Getter">@Getter</a><br>public class TransactionInfo {<br>    private String name;<br>    private String id;<br>    private double amount;<br>    private String note;<br>    private String currency;<br>}</pre><p>Then we have test like this:</p><pre><a href="http://twitter.com/Test">@Test</a><br>public void transactionTest() {<br>// Arrange<br>    TransactionInfo txn = new TransactionInfo();<br>    txn.setAmount(10000);<br>    txn.setCurrency(&quot;USD&quot;);<br>    txn.setNote(&quot;testing transaction&quot;);</pre><pre>    WebDriver driver = new ChromeDriver();<br>    TransactionPage txnPage = new TransactionPage(driver);<br>    TransactionResultPage txnResultPage = new TransactionResultPage(driver); // this page will fully display all transaction info</pre><pre>// Act<br>    String txnId = txnPage.make(txn);<br>    txn.setId(txnId);</pre><pre>    TransactionInfo actualTxn = txtResultPage.getTransaction();</pre><pre>// Assert<br>    Assert.assertEquals(txn, actualTxn);<br>}</pre><p>Now state contains properties of TransactionInfo object. As you can see:</p><ul><li>Some properties of txn are defined first, in <em>Arrange</em> stage.</li><li>Then txn variable are passed to make()method to get back transaction id and set to the idof txn</li><li>Next is getting a new instance of transaction info in txtResultPage.getTransaction().</li><li>Finally, compare state txn with actualTxn. Because putting assert command in page object is not good practice, so we will get transaction from page object level to test level.</li></ul><h3>Summary</h3><p>Hopefully, after observing the 2 examples above, you now have a clear view of state management in automation testing. There are some principles that you can follow:</p><ul><li>State values should live within only 1 test case, because all test cases should be independent of each other.</li><li>State can be created at the beginning of a test case, and added or edited during the test steps.</li><li>State can be shared between steps in a scenario, but should not be shared between pages directly. If you want to transfer state from page 1 (of step 1) to page 2 (of step 2), page 1 should return to step 1, and then step 2 will take state and pass it to page 2.</li></ul><pre>- Don&#39;t: page 1 -&gt; page 2<br>- Do: page 1 -&gt; step 1 -&gt; step 2 &gt; page 2</pre><ul><li>Stage is normally provided by steps and consumed by pages. Sometimes, the state comes from returned values of page object like Example 2 getTransaction().</li></ul><p><em>Original post from </em><a href="https://tandt53.com/state-management-in-automation-test-scenario"><em>https://tandt53.com/state-management-in-automation-test-scenario</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b0ac27924ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cách viết test case automation hiệu quả]]></title>
            <link>https://tandt53.medium.com/c%C3%A1ch-vi%E1%BA%BFt-test-case-automation-hi%E1%BB%87u-qu%E1%BA%A3-6d22e57ff9a6?source=rss-50bf1a8b68d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d22e57ff9a6</guid>
            <category><![CDATA[selenium]]></category>
            <category><![CDATA[effective]]></category>
            <category><![CDATA[test-case]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[automation-testing]]></category>
            <dc:creator><![CDATA[tandt53]]></dc:creator>
            <pubDate>Wed, 15 Dec 2021 10:28:41 GMT</pubDate>
            <atom:updated>2021-12-18T05:15:18.296Z</atom:updated>
            <content:encoded><![CDATA[<p>Gần đây mình nhận được 1 số câu hỏi của các bạn về việc viết automation test, như làm sao để viết test case cho đúng? đặt assert ở đâu cho phù hợp? etc. Trong quá trình mình làm automation cũng review và nhiều bạn cũng gặp khó khăn trong việc tổ chức các test case và page object sao cho phù hợp. Vậy nên mình sẽ có 1 số bài về các chủ đề này. Và đây là topic đầu tiên: làm để viết 1 test case automation hiệu quả, đầy đủ và hợp lý.</p><h3>Requirement</h3><p>Đầu tiên ta sẽ đưa ra 1 yêu cầu về nghiệp vụ như sau: Là 1 user, tôi muốn tính tổng 2 số tự nhiên, kết quả được hiển thị trên màn hình.<br>Developer sẽ phải viết 1 method để thực hiện yêu cầu kia, và viết cả unit test cho method đó nữa.<br>Tester sẽ phải test tính năng cộng này trên giao diện website hoặc mobile.<br>*** Tạm bỏ qua các developer front end khác để sử dụng method kia với front end nhé.</p><h3>Developer viết Unit Test như thế nào?</h3><p>Junit và TestNG là 2 thư viện cho việc viết unit test mà developer thường làm. Họ sử dụng 2 thư viện này để viết các test case cho các method hay class trong thư viện/framework/application của họ.<br>Annotation cơ bản và giống nhau của Junit và TestNG đều là @Test để xác định 1 method test, phân biệt với các method khác, không phải là method test. Hay nôm na, ta hiểu nó là 1 test case.<br>Trong method test, các developer thường có những quy chuẩn về việc nên sắp xếp các câu lệnh, hay gọi các method khác để phục vụ các nhiệm vụ nhất định. Quy chuẩn thông thường mà developer hay dùng đó là AAA. 3 chữ A tương ứng là:</p><ul><li>Arrange: là cài đặt hay khai báo các giá trị biến ở thời điểm ban đầu, hoặc cũng có thể là thiết lập 1 trạng thái nào đó trước khi bắt đầu các bước tiếp theo.</li><li>Act: Là các bước chính cho 1 test case. Tại đây gọi tới các method chính mà mình mong muốn để test (method under test).</li><li>Assert: kiểm tra hay verify kết quả trả về của method hoặc trạng thái sau khi gọi tới method cần test.<br>Ví dụ: ta có 1 hàm tính tổng và ta có 1 hàm unit test cho nó</li></ul><pre>// Method under test<br>public int sum(int a, int b){<br>  return a + b;<br>}<br><br>// Method test <br>@Test<br>public void happyCase(){<br>  // Arrange <br>  int a = 4;<br>  int b = 6;<br>  int result;<br>  int expected = 10;<br><br>  // Act <br>  result = sum(a, b);<br><br>  // Assert<br>  Assert.assertEquals(result, expected)<br><br>}</pre><h3>Tester viết test case và automation như thế nào?</h3><p>Giờ ta trở lại với công việc thường ngày của 1 tester hay phải tiếp xúc đó là test case trên excel hay test management system nào đó.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7492619b8d8c01e58d2300a439396b17/href">https://medium.com/media/7492619b8d8c01e58d2300a439396b17/href</a></iframe><p>Khi chuyển sang automation test, những điều ta cần quan tâm nhất đó chính là:<br>- Điều kiện tiên quyết<br>- Các bước thực hiện hay nghiệp vụ<br>- Kết quả mong muốn<br>Các thông tin khác của test case chỉ mang tính bổ sung và ta sẽ tạm bỏ qua ở bài viết này. Giờ ta sẽ xem việc chuyển test case trên sang automation.</p><pre>@Test<br>public void happyCase(){<br>  // Arrange <br>  System.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;chromedriver.exe&quot;);<br>  WebDriver driver = new ChromeDriver();<br>  CalculationPage page = new CalculationPage(driver);<br>  int result;<br>  int a = 4;<br>  int b = 6;<br>  int expected = 10;<br><br>  // Act<br>  page.sum(4, 6);<br>  resul = page.getResult();<br><br>  // Assert <br>  Assert.assertEquals(result, expected)<br><br>  // Release<br>  driver.quit();<br>}</pre><p>Hãy xem trong test case trên ta có đủ 3 thành phần AAA, và xuất hiện thêm 1 thành phần nữa, đó là Release. Lý do ta cần có những câu lệnh trong phần release này là gì? Tại sao unit test lại không cần?</p><p>Câu trả lời đó là vì automation test sử dụng selenium là integration (hoặc system test). Sau khi kết thúc 1 kịch bản test, ta nên trả lại trạng thái ban đầu của hệ thống under test để tránh ảnh hưởng tới test case khác, giúp cho các test case được độc lập với nhau. Phần này không bắt buộc nhưng đó là điểm ta cần lưu ý mỗi khi viết automation test case. Unit test có thể không cần vì đối tượng chỉ là 1 hàm, chứ không phải là trạng thái của hệ thống.</p><h3>Finalize</h3><ul><li>Structure nên follow theo quy tắc 3 chữ AAAR: Arrange - Act - Assert - Release</li><li>Test case automation là thể hiện của business requirement chứ không phải là chỉ dẫn từng bước cho việc test.</li></ul><pre>@Test<br>public void loginTest(){<br><br>  // Act: không nên <br>  loginPage.enterUsername(&quot;username&quot;);<br>  loginPage.enterPassword(&quot;password&quot;);<br>  loginPage.clickLogin();<br><br>  // Act: nên <br>  loginPage.login(&quot;username&quot;, &quot;password&quot;); // method login() in LoginPage class chứa 3 câu lệnh như trên<br><br>}</pre><ul><li>Một test case chỉ nên có một điểm verify duy nhất để đảm bảo đúng với test purpose. Ta chỉ nên verify 1 hoặc 1 vài checkpoint ở bước Assert. Chứ không nên mix Act - Assert - Act - Assert.</li><li>Test case automation nên được dừng ngay khi có vấn đề phát sinh. Vấn đề có thể là assert bị fail, hoặc là vấn đề liên quan tới nghiệp vụ không được thực hiện đúng như ý muốn (do UI hay flow của hệ thống thay đổi, ví dụ như vấn đề gây ra bởi NoSuchElementException….).</li><li>Không đặt assert trong Page Object.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d22e57ff9a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[FOM thay thế POM trong automation test]]></title>
            <link>https://tandt53.medium.com/fom-thay-th%E1%BA%BF-pom-trong-automation-test-f597522af6c9?source=rss-50bf1a8b68d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/f597522af6c9</guid>
            <category><![CDATA[pom]]></category>
            <category><![CDATA[fom]]></category>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[appium]]></category>
            <category><![CDATA[selenium]]></category>
            <dc:creator><![CDATA[tandt53]]></dc:creator>
            <pubDate>Sun, 15 Mar 2020 15:40:11 GMT</pubDate>
            <atom:updated>2020-03-15T15:40:11.350Z</atom:updated>
            <content:encoded><![CDATA[<h3>POM (Page Object Model)</h3><p>Thuật ngữ POM đã xuất hiện và sử dụng rất lâu, phổ biến trong các project automation test (AT). Lợi ích của việc sử dụng model này đó là tách biệt việc quản lý Page hay Element trên Page và Test case để có thể tái sử dụng. Tuy nhiên, trong việc phát triển sản phẩm hiện đại ngày nay, mô hình POM đang giới hạn các Element trong 1 màn hình (Page). Điều này dẫn tới 1 số hạn chế sau:</p><ul><li>Việc phát triển AT chỉ đc thực hiện sau khi UI được triển khai.</li><li>Khi 1 element trên Page thay đổi (thêm, sửa, xoá hay chuyển sang Page khác) thì các page class sẽ phải thay đổi theo.</li><li>Nếu việc tái sử dụng layout được áp dụng phổ biến trong Application, thì mô hình POM sẽ gây ra lặp Element và các method liên quan tới các Element đó.</li></ul><h3>Mô hình nào sẽ thay thế POM?</h3><p>Thời gian gần đây, việc phát triển các test case automation không sử dụng POM xuất hiện nhiều hơn với mô hình Screen Play, mà framework điển hình đó là Serenity.</p><p>Tuy nhiên, việc cố tình ép chia các method nhỏ ra trong 1 page ra thành các class khác nhau sẽ gặp khó khăn cho việc quản lý các class hay method đó.</p><p>Dựa vào kinh nghiệm làm việc với POM và các dự án thực tế dành cho Mobile Automation Test, đồng thời với việc nghiên cứu các mô hình hay chuẩn (pattern) phát triển sản phẩm hiện nay, tôi nhận thấy việc phát triển ứng dụng đang có xu hướng như sau:</p><ul><li>Everything-As-Code: các bước phát triển phần mềm hay build, deploy đều thiên hướng tới việc sử dụng coding hay scripting để thực hiện, đặc biệt rõ rệt nhất là việc quản lý và triển khai hạ tầng cho các sản phẩm phần mềm (Infra as Code) sử dụng rộng rãi các config dạng format .yml.</li><li>Design as Code: việc thiết kế giao diện là 1 bước quan trọng để có thể hiển thị UI của sản phẩm cho người dùng cuối có thể sử dụng. Công đoạn này đang được coding hoá, ví dụ Android layout xml, Flutter &amp; Dart, SwiftUI. Layout được thiết kế và triển khai bằng việc sử dụng ngôn ngữ lập trình nhất định, và việc thao tác hay thay đổi giá trị cho từng layout được implement nội tại trong code. Thường được tập trung vào từng thành phần của business flow (ví dụ như concept BLOC trong flutter), để hạn chế việc thủ công update state và value của 1 element, và tận dụng khả năng tái sử dụng, lắp đặt trên các layout khác nhau.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r7i2x9l4RE0e-cGGirMyOg.png" /></figure><p>Điều gì sẽ xảy ra nếu ứng dụng của bạn sử dụng 1 layout nhưng lại xuất hiện ở 2 màn hình khác nhau? Được đặt trong 2 container (full &amp; half screen) khác nhau? Bạn sẽ phải dupplicate code khai báo cho các element, các method? Hoặc nếu bạn tổ chức ở 1 class thì sẽ phải tính sao nếu như ở Half screen layout có thêm 1 layout ở nửa trên? Lúc đó Page Object sẽ nằm trong Page Object khác? Như vậy liệu có thuận tiện cho việc quản lý page object?</p><p>Sẽ có rất nhiều câu hỏi và thật khó có thể làm thoả mãn được hết các yêu cầu bởi vì <strong><em>1 element hay 1 layout phải gắn cứng với 1 page object nào đó</em></strong>. Nhưng với mỗi layout sẽ được developer sử dụng rất linh hoạt, không phụ thuộc vào 1 màn hình (page) nào. Vậy layout phải gắn với object nào để có thể linh hoạt như việc developer sử dụng?</p><p>Có 1 điểm dễ nhận thấy rằng: <strong><em>layout chứa các element, và các thao tác hay giá trị trên các element đó, chỉ phụ thuộc vào business flow hay user story</em></strong>. Một business flow cần hiển thị data và thao tác trên giao diện như thế nào thì sẽ tương ứng gọi tới layout thích hợp.</p><p>Đó là lý do một mô hình mới được đề xuất cho phù hợp hơn: <strong>Flow Object Model (FOM)</strong></p><ul><li>Đối tượng của mô hình này là Flow thay vì Page.</li><li>Trong 1 flow sẽ bao gồm các thành phần UI layout đặc thù cho flow đó, Data gắn trên layout đó (nếu cần).</li><li>Một layout có thể xuất hiện 1 hoặc nhiều Page và thuộc các container khác nhau, do vậy FOM có thể giúp cho việc tổ chức layout thuộc flow được linh hoạt hơn, và hạn chế việc lặp lại (DRY).</li><li>Data trong flow có thể xuất hiện 1 hay nhiều lần ở nơi mà layout hiển thị.</li></ul><h3>Ví dụ</h3><p>Hãy thử lấy 1 ví dụ đơn giản như sau với 1 luồng business mua và thanh toán đơn hàng gồm 3 step sau</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MPFueaRaD0tDKQbnQEjL0Q.png" /></figure><ul><li>Step 1: Hiển thị và lựa chọn các item cần mua.</li><li>Step 2: Hiển thị các item đã chọn, các phương thức thanh toán, và thực hiện thanh toán. Các giá trị bao gồm: tổng giá của các item (<em>items amount</em>), phí thanh toán (<em>fee</em>) và tổng số tiền cần thanh toán (<em>total amount</em>).</li><li>Step 3: Hiển thị các item đã chọn, và trạng thái đơn hàng đã thanh toán thành công hay thất bại. Các giá trị bao gồm: tổng số tiền đã thanh toán, trạng thái thanh toán của đơn hàng (<em>status</em>), mã đơn hàng (<em>code</em>).</li></ul><p>Với kịch bản này thông thường sẽ phải tạo 3 object (~3 class) trong đó việc lặp lại các element/layout cho việc hiển thị danh sách các item.</p><p>Để áp dụng mô hình FOM, ta cần phân tích với luồng business này:</p><ul><li>3 màn hình gồm step 1, step 2 và step 3.</li><li>3 layout gồm List of items, Payment Options, Payment Result.</li><li>2 data object gồm thông tin của các item (<em>ItemInfo</em>), thông tin của việc thanh toán (<em>PaymentInfo</em>).</li></ul><p>Nhận thấy các thông tin của <em>PaymentInfo </em>được hiển thị ở 2<em> </em>layout <em>Payment Options</em> và <em>Payment Result</em>. Do vậy, ta chỉ cần 2 Flow Object dựa trên 2 data object trên.</p><ul><li>Item Flow: lựa chọn item, kiểm tra danh sách item sau khi lựa chọn, kiểm tra danh sách item sau khi thanh toán xong</li><li>Payment Flow: thanh toán, so sánh tổng giá trị đơn hàng (totalAmount) ở step 2 và tổng tiền đã thanh toán hiển thị ở step 3.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Os9UqHlY9LfQEwWNZll7tQ.png" /></figure><p>Bây giờ ta sẽ bắt tay vào việc triển khai code cho kịch bản này. Đầu tiên là tạo các package và class như sau:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*45_MMUxrhQ8ovzGgsHKUZA.png" /></figure><p>Triển khai code cho package model:</p><ul><li><em>ItemInfo</em>: chứa thông tin cơ bản của item như tên và giá.</li></ul><pre>public class ItemInfo {<br>    private String name;<br>    private double price;<br><br>    public ItemInfo(String name) {<br>        this.name = name;<br>    }<br><br>    public String getName() {<br>        return name;<br>    }<br><br>    public void setName(String name) {<br>        this.name = name;<br>    }<br><br>    public double getPrice() {<br>        return price;<br>    }<br><br>    public void setPrice(double price) {<br>        this.price = price;<br>    }<br>}</pre><ul><li><em>PaymentInfo</em>: bao gồm các thông tin tổng giá của các items đã chọn, phí thanh toán, tổng giá trị của đơn hàng, kết quả thanh toán và mã đơn hàng.</li></ul><pre>public class PaymentInfo {<br>    private double itemsAmount;<br>    private double fee;<br>    private double totalAmount;<br>    private String status; // SUCCESS, FAIL, PROCESSING<br>    private String code;<br><br>    public double getItemsAmount() {<br>        return itemsAmount;<br>    }<br><br>    public void setItemsAmount(double itemsAmount) {<br>        this.itemsAmount = itemsAmount;<br>    }<br><br>    public double getFee() {<br>        return fee;<br>    }<br><br>    public void setFee(long fee) {<br>        this.fee = fee;<br>    }<br><br>    public double getTotalAmount() {<br>        return totalAmount;<br>    }<br><br>    public void setTotalAmount(long totalAmount) {<br>        this.totalAmount = totalAmount;<br>    }<br><br>    public String getStatus() {<br>        return status;<br>    }<br><br>    public void setStatus(String status) {<br>        this.status = status;<br>    }<br><br>    public String getCode() {<br>        return code;<br>    }<br><br>    public void setCode(String code) {<br>        this.code = code;<br>    }<br>}</pre><p>Tiếp theo là triển khai cho phần flow:</p><ul><li><em>ItemFlow</em>: bao gồm các method lựa chọn item ở step 1 và lấy các thông tin item đã được lựa chọn ở step 2 hay step 3.</li></ul><pre>public class ItemsFlow {<br><br>    public void selectItem(ItemInfo itemInfo) {<br>        // implement code here<br>    }<br><br>    public List&lt;ItemInfo&gt; getSelectedItems() {<br>        // implement code here<br>        return null;<br>    }<br><br>}</pre><ul><li><em>PaymentFlow</em>: bao gồm việc lấy thông tin thanh toán ở step 2, xác nhận thanh toán, và lấy thông tin thanh toán ở step 3.</li></ul><pre>public class PaymentFlow {<br><br>    public PaymentInfo getPaymentOptions(){<br>        // implement code here<br>        return null;<br>    }<br>    <br>    public void confirm(){<br>        // implement code here<br>    }<br>    <br>    public PaymentInfo getPaymentResult(){<br>        // implement code here<br>        return null;<br>    }<br>}</pre><p>Và bước cuối cùng là <em>SampleTest</em>:</p><pre>public class SampleTest {<br><br>    public static void main(String[] args) {<br>        // 1. At Step 1<br>        // init item to be selected<br>        ItemInfo item01 = new ItemInfo(&quot;item01&quot;);<br>        ItemInfo item02 = new ItemInfo(&quot;item02&quot;);<br>        ItemInfo item03 = new ItemInfo(&quot;item03&quot;);<br><br>        List&lt;ItemInfo&gt; itemsList = new ArrayList&lt;&gt;();<br>        itemsList.add(item01);<br>        itemsList.add(item02);<br>        itemsList.add(item03);<br><br>        // Select list of initialized items<br>        ItemsFlow itemsFlow = new ItemsFlow();<br>        itemsFlow.selectItem(item01);<br>        itemsFlow.selectItem(item02);<br>        itemsFlow.selectItem(item03);<br><br>        // 2. At step 2<br>        // 2.1. Verify actual selected item with given list of items above<br>        Assert.<em>assertEquals</em>(itemsFlow.getSelectedItems(), itemsList);<br><br>        // calculate the price of all item and fee<br>        // payment fee could be 10% of that price<br>        double itemPrice01 = item01.getPrice();<br>        double itemPrice02 = item02.getPrice();<br>        double itemPrice03 = item03.getPrice();<br>        double itemsPrice = itemPrice01 + itemPrice02 + itemPrice03;<br>        double fee = itemsPrice * 0.1;<br>        double totalAmount = itemsPrice + fee;<br><br>        // 2.2. Verify total price of all selected items and the price display in Payment options<br>        PaymentFlow paymentFlow = new PaymentFlow();<br>        paymentFlow.confirm();<br><br>        Assert.<em>assertEquals</em>(paymentFlow.getPaymentOptions(), totalAmount);<br><br>        // 3. At step 3<br>        // Verify the list of selected items and payment result<br>        Assert.<em>assertEquals</em>(itemsFlow.getSelectedItems(), itemsList);<br><br>        String status = &quot;SUCCESS&quot;; // depends on the status that you expect<br>        Assert.<em>assertEquals</em>(paymentFlow.getPaymentResult().getStatus(), status);<br>    }<br>}</pre><p>Trên đây là 1 ví dụ điển hình cho việc áp dụng mô hình FOM trong AT với điểm mạnh ở việc số class/method gọn hơn và linh hoạt hơn so với POM.</p><p><strong>Kết luận</strong></p><p>Tóm lại, việc tổ chức và triển khai code AT theo FOM dựa trên những nguyên tắc sau:</p><ul><li>Trong 1 business flow thường sẽ đi qua nhiều Page khác nhau với các <strong>layout</strong> và <strong>data</strong> nhất định.</li><li>Layout và Data là 2 object được gắn với nhau chặt chẽ, số lượng layout object để hiển thị các thông tin thường nhiều data object hơn.</li><li>Trong 1 business flow có thể sẽ có 1 hoặc nhiều data object (như trên có Item và Payment). Các object thường có mối liên hệ nhất định, ta có thể dựa vào đó để khởi tạo các giá trị làm input và trả về các giá trị output khi cần.</li></ul><p>Một số hướng phát triển cho FOM:</p><ul><li>Việc áp dụng mô hình FOM có thể hướng tới việc triển khai AT sớm hơn, có thể start cùng với việc development. Bởi vì, tester chỉ cần quan tâm business flow và không cần biết element/layout phải đặt ở Page nào.</li><li>FOM hỗ trợ việc đóng gói tốt hơn so với POM. Ta hoàn toàn có thể đặt cả 1 flow trong 1 method với input parameter và output data nhất định.</li><li>Việc áp dụng mô hình FOM sẽ tiết kiệm thời gian cho việc update test code hơn POM, đặc biệt là cho layout. Vì layout có thể thay đổi từ Page này sang Page khác, hay container này sang container khác, nhưng layout đó vẫn thuộc 1 business flow đã được đinh sẵn.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f597522af6c9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>