Latest / CyberCode Academy / Course 37 - Building Web Apps with Ruby On Rails | Episode 13: From Initial Setup to Advanced UI Interaction
Transcript
- 0:00Speaker 1: You know, I want you to imagine. Imagine you're opening
- 0:03Speaker 1: a brand new restaurant. Oh nice, yeah, right, and you
- 0:06Speaker 1: hire this incredibly rigorous health inspector to come in and
- 0:11Speaker 1: they meticulously check the kitchen. I mean, they test the
- 0:14Speaker 1: temperature calibration of the ovens, they verify every single ingredient
- 0:18Speaker 1: is perfectly fresh, and they make sure your chefs are
- 0:20Speaker 1: following every recipe to the exact gram.
- 0:23Speaker 2: So basically, the kitchen gets a perfect flawless score. Exactch
- 0:27Speaker 2: means you're theoretically ready for business, like the internal operations
- 0:31Speaker 2: are mathematically.
- 0:32Speaker 1: Perfect theoretically yes, yeah, But then opening night comes around.
- 0:36Speaker 1: A hungry customer walks up to the front door, reaches
- 0:39Speaker 1: for the handle, and it's been installed on the wrong side.
- 0:42Speaker 1: Oh no, yeah, they literally cannot get inside the building.
- 0:46Speaker 1: That perfect kitchen doesn't matter at all because the user
- 0:48Speaker 1: experience is entirely blocked by this physical barrier that the
- 0:53Speaker 1: kitchen inspector just wasn't hired to look for. You're locked out.
- 0:56Speaker 2: That's a brutal opening night.
- 0:58Speaker 1: It really is. Yeah, And that scenario is exactly what
- 1:01Speaker 1: happens in software development when we rely purely on traditional
- 1:05Speaker 1: back end testing. You know, we verify the engine runs perfectly,
- 1:08Speaker 1: but we just completely forget to check if the steering
- 1:11Speaker 1: wheel actually works right, the human element. Right. So today,
- 1:14Speaker 1: in this deep dive, we're going to break down how
- 1:16Speaker 1: to build the ultimate ghost user to automate that user
- 1:19Speaker 1: experience in rubyond rails. We're going to turn manual clicking
- 1:23Speaker 1: into an automated superpower and really future proof your code base. Okay,
- 1:27Speaker 1: let's unpack this.
- 1:28Speaker 2: I love that restaurant analogy because it highlights a really
- 1:30Speaker 2: critical shift in perspective. We're going to bridge the gap
- 1:34Speaker 2: today between writing the raw logic and actually guaranteeing a
- 1:38Speaker 2: flawless user experience because developers often fall into what we
- 1:42Speaker 2: might call the illusion of perfect.
- 1:44Speaker 1: Code, the illusion of perfect code.
- 1:46Speaker 2: Yeah, you can have this web application where the core
- 1:48Speaker 2: business logic is covered end to end by unit tests
- 1:51Speaker 2: and integration tests. I mean, the HGTP requests are firing,
- 1:54Speaker 2: the controllers are routing the data properly, the database is
- 1:57Speaker 2: saving records.
- 1:58Speaker 1: But those tests have a massive blind spot, don't they
- 2:01Speaker 1: a huge one because they're really just checking the data flow. Right.
- 2:04Speaker 1: A unit test hits the database directly, it bypasses the
- 2:07Speaker 1: network stack. It bypasses the browser entirely. It's like taking
- 2:12Speaker 1: the engine out of a car, putting it on a
- 2:14Speaker 1: workbench and just proving that it sparks right. Great, But
- 2:18Speaker 1: that workbench test completely misses the human element, the actual
- 2:22Speaker 1: user experience.
- 2:23Speaker 2: Because I mean, a human being doesn't interact with Jason
- 2:26Speaker 2: payloads or server side routing. They interact with the visual interface,
- 2:30Speaker 2: the document object model, or DOM. You could have an
- 2:34Speaker 2: integration test that passes with flying colors, verifying that a
- 2:38Speaker 2: specific submit button works perfectly.
- 2:41Speaker 1: On the back end, but then what happens in the browser.
- 2:44Speaker 2: Well, when that code renders in an actual web browser,
- 2:47Speaker 2: that crucial button might be totally unclickable, like it could
- 2:49Speaker 2: be accidentally covered up by a floating navigation bar, or
- 2:52Speaker 2: maybe it got pushed entirely offscreen by a broken CSS grid, and.
- 2:55Speaker 1: A traditional integration test just won't catch that Z index overlap,
- 2:59Speaker 1: not at all, because it doesn't have a screen. It
- 3:02Speaker 1: literally doesn't see visual layout. And that is exactly where
- 3:05Speaker 1: system tests come in, you know, often called end to
- 3:07Speaker 1: end or E two E tests. We are essentially writing
- 3:11Speaker 1: code that summons an invisible robot to open a real
- 3:14Speaker 1: web browser act exactly like a human click around and
- 3:18Speaker 1: prove the application is actually.
- 3:20Speaker 2: Usable, that robot is invaluable, I mean truly. But it
- 3:23Speaker 2: does raise a pretty significant architectural.
- 3:25Speaker 1: Dilemma, right, which makes me wonder if an E two
- 3:28Speaker 1: E test is so much better at catching these real
- 3:30Speaker 1: world layout bugs, why don't we just completely abandon the workbench, Like,
- 3:34Speaker 1: why not write only E two E tests and guarantee
- 3:37Speaker 1: every single function works for the user?
- 3:39Speaker 2: Oh that's the temptation. Every new developer thinks that, But
- 3:41Speaker 2: it really comes down to computational physics.
- 3:44Speaker 1: Physics.
- 3:44Speaker 2: Yeah, E two E tests are incredibly thorough, but they
- 3:47Speaker 2: are incredibly heavy. To run a unit test, your computer
- 3:50Speaker 2: just executes a few lines of Ruby in memory it
- 3:52Speaker 2: takes milliseconds. But to run an E two E test,
- 3:55Speaker 2: the system has to spin up an entire Chromium browser process,
- 3:59Speaker 2: wait for the JavaScript dungeine to pars, render the CSS,
- 4:02Speaker 2: and then calculate the visual layout for every single page load.
- 4:05Speaker 1: Wow. Okay, so as an order of magnitude slower.
- 4:08Speaker 2: Oh, several orders of magnitude. If you have say, five
- 4:11Speaker 2: thousand tests, running them all as unit tests might take
- 4:13Speaker 2: thirty seconds. Running them all is E two E tests.
- 4:17Speaker 2: That would literally take hours.
- 4:19Speaker 1: Oh wow, Yeah, your development pipeline would just grind to
- 4:22Speaker 1: a total halt exactly.
- 4:23Speaker 2: So the industry standard is more of a permit approach.
- 4:26Speaker 2: You want a massive base of lightning fast unit tests
- 4:29Speaker 2: for granular logic, and then a smaller capstone of E
- 4:33Speaker 2: two E tests for those broad critical user journeys like
- 4:36Speaker 2: logging in or checking out.
- 4:38Speaker 1: That makes total sense. You only use the heavy machinery
- 4:40Speaker 1: where you really need it. So how do we actually
- 4:44Speaker 1: summon this heavy machinery? How do we get this ghost
- 4:47Speaker 1: user to take the wheel in a Ruby on Rails environment.
- 4:50Speaker 2: So Rails relies on a library called cappy barret to
- 4:53Speaker 2: handle this. Capybar basically acts as the translation layer between
- 4:57Speaker 2: your Ruby code and the actual browser. To physically drive
- 5:00Speaker 2: the browser, Capybar needs a tool called Selenium Webdriver.
- 5:03Speaker 1: And here is where developers often hit their very first roadblock,
- 5:07Speaker 1: right because Rails does not just hand you this driver
- 5:10Speaker 1: out of the box.
- 5:10Speaker 2: No, it definitely doesn't.
- 5:12Speaker 1: You generate your test file by running Rails generate system
- 5:15Speaker 1: testing your terminal, and then you try to run it
- 5:17Speaker 1: and immediately crash. Yeah, it throws an error complaining about
- 5:21Speaker 1: a missing webdriver executable.
- 5:22Speaker 2: What's fascinating here is how this setup forces the developer
- 5:26Speaker 2: to understand the boundary between their application and the host
- 5:29Speaker 2: operating system. Because Rails cannot bundle the webdriver, the driver
- 5:34Speaker 2: has to perfectly match the specific browser version installed on
- 5:38Speaker 2: that developer's unique machine.
- 5:40Speaker 1: Right if I'm on a Windows machine running Chrome version
- 5:42Speaker 1: eighty seven, I have to literally step outside of my
- 5:45Speaker 1: code editor, go to some Google API site, manually download
- 5:49Speaker 1: the Chrome eighty seven dot ex file and add it
- 5:52Speaker 1: to my system's path variables exactly.
- 5:54Speaker 2: The Ruby environment has to know exactly where that binary lives,
- 5:57Speaker 2: so it can physically launch the browser Apple cation right
- 6:00Speaker 2: there on your desktop.
- 6:01Speaker 1: It's orchestrating external software. And once you finally have that
- 6:04Speaker 1: binary wired up, there's another quirk to actually executing the tests,
- 6:08Speaker 1: because if a developer types the standard Rails test command,
- 6:12Speaker 1: Rails will intentionally ignore all the system tests.
- 6:15Speaker 2: They just wrote, which goes right back to that speed
- 6:17Speaker 2: issue we talked about. Rails assumes that if you're just
- 6:20Speaker 2: typing Rails tests, you want quick feedback on your logic.
- 6:23Speaker 2: It doesn't want to accidentally launch a thirty minute browser
- 6:25Speaker 2: testing suite when you just changed a typo.
- 6:27Speaker 1: Right if you want the ghost user, you have to
- 6:29Speaker 1: explicitly invite it. You have to run rails test dot system,
- 6:33Speaker 1: or you target one specific file precisely.
- 6:36Speaker 2: So let's assume we've run the command. The terminal spins up,
- 6:39Speaker 2: capybar talks to Selenium. Selenium launches the browser binary. Boom,
- 6:42Speaker 2: the window opens. The ghost user is sitting there in
- 6:45Speaker 2: the driver's seat.
- 6:46Speaker 1: But just staring at the screen doesn't help if it
- 6:48Speaker 1: doesn't know what to do. If we wanted to test
- 6:51Speaker 1: our registration flow, how does it actually know what to
- 6:53Speaker 1: click or where to type?
- 6:55Speaker 2: It needs signposts. Capybar reads the dom the underlying structure
- 6:59Speaker 2: of the HTML, and it navigates using specific actions. For instance,
- 7:03Speaker 2: click on is a command that tells the test to
- 7:05Speaker 2: look for a button or a link with a specific label,
- 7:08Speaker 2: and fillin is used to type text into specific input fields.
- 7:12Speaker 1: But wait a minute. If I tell this automated test
- 7:15Speaker 1: to click in a button called edit profile, right, and
- 7:18Speaker 1: I'm looking at an admin dashboard with a table of
- 7:21Speaker 1: let's say fifty different users.
- 7:23Speaker 2: Oh, I see where you're going.
- 7:25Speaker 1: Doesn't every single row in that table have an edit
- 7:28Speaker 1: profile button? How does capybar know which one I mean?
- 7:31Speaker 1: Wouldn't it just click the first one it sees and
- 7:34Speaker 1: completely ruin the flow of the test.
- 7:36Speaker 2: It absolutely would. That is a classic scope problem. The
- 7:39Speaker 2: automated test is literal to a fault. If there's ambiguity,
- 7:42Speaker 2: it either clicks the wrong element or it just fails
- 7:44Speaker 2: out of confusion. To fix this, you have to give
- 7:47Speaker 2: it physical boundaries using a within block.
- 7:49Speaker 1: So you essentially draw a box on the screen exactly.
- 7:52Speaker 2: You tell the test look within the HTML element that
- 7:55Speaker 2: corresponds to user number forty two, and then find the
- 7:58Speaker 2: edit profile button. It skrips the action.
- 8:00Speaker 1: Okay, that makes sense.
- 8:01Speaker 2: But to even do that, developers often have to go
- 8:04Speaker 2: back into their front end source code and add specific
- 8:06Speaker 2: CSS classes just to make those areas queriable. Like you
- 8:10Speaker 2: might add a class simply called user row to your HTML,
- 8:13Speaker 2: not for styling, but just so your test has a
- 8:16Speaker 2: reliable signpost to find.
- 8:17Speaker 1: It's like a symbiotic relationship. You're altering the architecture of
- 8:21Speaker 1: the app slightly just to make it testable. But even
- 8:24Speaker 1: with those signposts, I know there are some wild syntax
- 8:26Speaker 1: gotchas that can drive a developer completely crazy. Oh absolutely,
- 8:31Speaker 1: Let's talk about the fill in command. Say you want
- 8:33Speaker 1: the ghost user to type a username into an input field.
- 8:36Speaker 1: The input field in the HTML has an idea of username,
- 8:40Speaker 1: so naturally you write your test to target hashtag username
- 8:44Speaker 1: using the hash. You run it and it fails. You
- 8:47Speaker 1: can't find it. Yeah.
- 8:48Speaker 2: This trips up almost everyone migrating from front end development
- 8:51Speaker 2: over to testing because in CSS, developers are deeply trained
- 8:55Speaker 2: that the hash symbol the hashtag denotes an ID. But
- 8:58Speaker 2: copybar's filling command. It doesn't actually use CSS selectors. It
- 9:02Speaker 2: uses something called a locator.
- 9:03Speaker 1: And what's the difference under the hood, Like, why does
- 9:05Speaker 1: it flat out reject the.
- 9:07Speaker 2: Hash symbol Because a locator is specifically designed to mimic
- 9:11Speaker 2: how a human thinks, not how a style sheet thinks.
- 9:13Speaker 1: Oh interesting, yeah.
- 9:14Speaker 2: CSS selector scan the styling layer. A locator scans the
- 9:18Speaker 2: semantic HTML. It looks for accessibility labels, name attributes, or
- 9:22Speaker 2: raw ID strings. So when you pass it hashtag username
- 9:26Speaker 2: with the hash, it takes you completely literally, it searches
- 9:29Speaker 2: the page for an element whose actual raw name is
- 9:33Speaker 2: literally hashtag username, which obviously doesn't exist. You have to
- 9:37Speaker 2: strip the hash out and just provide the string username.
- 9:40Speaker 1: That makes so much sense when you explain the mechanism
- 9:42Speaker 1: behind it. Yes, it's trying to act like a user
- 9:44Speaker 1: reading a label, not a browser applying a font color.
- 9:47Speaker 1: And you see a similar friction point with email addresses.
- 9:50Speaker 2: Right well, the at symbol.
- 9:51Speaker 1: Yeah, if you try to use an email address as
- 9:53Speaker 1: a selector to find a specific element, the at symbol
- 9:56Speaker 1: totally breaks the query parser. You have to pivot and
- 9:59Speaker 1: use a safe string like the user's profile name instead.
- 10:02Speaker 2: It's a constant reminder that we are working through a
- 10:04Speaker 2: really rigid translation layer, and that rigidity totally extends to
- 10:09Speaker 2: how the ghost user perceives time too.
- 10:11Speaker 1: Oh man, the timing issues. This is where ETOE tests
- 10:14Speaker 1: get really brittle. Let's say our ghost user successfully types
- 10:18Speaker 1: in the details and clicks the sign up button. The
- 10:20Speaker 1: application has to process that, save the user to the database,
- 10:23Speaker 1: and then render a new dashboard page.
- 10:26Speaker 2: Which takes a moment.
- 10:27Speaker 1: Right. That might take a second or two, especially if
- 10:29Speaker 1: it's shating an external API. But the automated test is
- 10:33Speaker 1: running at the speed of the CPU.
- 10:35Speaker 2: Right, the Ruby thread executes the click, and literally one
- 10:38Speaker 2: melisecond later, it executes the next line of code, which
- 10:41Speaker 2: says verify the dashboard is visible.
- 10:44Speaker 1: But the browser hasn't even finished the network.
- 10:46Speaker 2: Request yet exactly. The test looks for the dashboard, doesn't
- 10:50Speaker 2: see it, and instantly fails.
- 10:51Speaker 1: It's a raise condition. The test literally outran the application.
- 10:55Speaker 2: To mitigate this, capybar has a built in synchronization feature,
- 10:58Speaker 2: but you often have to explore manage it. You introduce
- 11:02Speaker 2: a weight ten parameter on your assertions. Now, this doesn't
- 11:06Speaker 2: mean the test stubbornly pauses doing nothing for exactly ten seconds.
- 11:10Speaker 1: Oh it doesn't.
- 11:11Speaker 2: No, it means it will continuously pull the down checking
- 11:15Speaker 2: over and over again for up to ten seconds, waiting
- 11:18Speaker 2: for that asynchronous JavaScript or that slow page load to finish,
- 11:22Speaker 2: before it throws its hands up and finally declares a failure.
- 11:25Speaker 1: Oh, I see. It gives the application room to breathe.
- 11:27Speaker 2: Exactly.
- 11:28Speaker 1: But let's say we've mastered the timing the tests for Pristine,
- 11:32Speaker 1: the ghost user navigates flawlessly. Here's a massive architectural question
- 11:36Speaker 1: for you. What happens when the underlying technology of our
- 11:39Speaker 1: application inevitably changes?
- 11:41Speaker 2: Ah the ultimate challenge of future proofing. If you don't
- 11:45Speaker 2: design your tests carefully, a simple UI update can shatter
- 11:48Speaker 2: your entire testing suite.
- 11:50Speaker 1: There's a brilliant scenario for this. Imagine you're building a
- 11:53Speaker 1: feature with user profile cards, kind of like a dating
- 11:56Speaker 1: app interface where you swipe left or right. You build
- 11:59Speaker 1: it using a popular JavaScript framework like viewjs, and you
- 12:03Speaker 1: import a third party library to handle the touch mechanics.
- 12:06Speaker 2: Right, a very standard development practice. You leverage existing tools, right.
- 12:10Speaker 1: But let's say the library generates an HTML container with
- 12:13Speaker 1: a highly specific class name, something like view card swipe container.
- 12:18Speaker 1: If you write your E TWOE test to look for
- 12:20Speaker 1: that exact class name view card swipe container, you're essentially
- 12:24Speaker 1: tying the survival of your test to a library you
- 12:27Speaker 1: don't even control.
- 12:28Speaker 2: And what happens in two years when that library is deprecated,
- 12:32Speaker 2: flagged for a security vulnerability, or simply replaced by something faster.
- 12:36Speaker 2: It breaks exactly If we connect this to the bigger picture,
- 12:39Speaker 2: this touches on the whole concept of the testing contract.
- 12:43Speaker 2: When you write a test that looks for a third
- 12:45Speaker 2: party class name, your contract is with that external library.
- 12:49Speaker 2: The moment you replace the library, the class name disappears
- 12:52Speaker 2: and your test fails. Even if the application itself still
- 12:55Speaker 2: works perfectly for the end user. You've created a false.
- 12:58Speaker 1: Negative and false negatives totally road trust in the testing suite.
- 13:02Speaker 1: Developers start ignoring field tests because they just assume they're brittle.
- 13:05Speaker 1: So how do we fix a contract?
- 13:07Speaker 2: We decouple the test from the implementation. The best practice
- 13:11Speaker 2: is to add your own generic, semantic class names to
- 13:14Speaker 2: the elements. Instead of targeting viewcard swipe container, you add
- 13:19Speaker 2: a generic class like user profile card and tell cappy
- 13:23Speaker 2: bar to look for that.
- 13:24Speaker 1: That way, the test only cares about your business logic.
- 13:28Speaker 1: The user doesn't care if the card is powered by
- 13:30Speaker 1: View or React or vanilla JavaScript. They just care that
- 13:33Speaker 1: the card is there and it swipes. By using a
- 13:36Speaker 1: generic class, the test adopts that exact same perspective. You
- 13:40Speaker 1: can rip out the entire front end framework, rebuild it
- 13:43Speaker 1: from scratch, and as long as you put that user
- 13:45Speaker 1: profile card class back on the new element, your test
- 13:48Speaker 1: will pass without a single line of test code changing.
- 13:51Speaker 2: Because you're testing the behavior, not the specific implementation. It's
- 13:55Speaker 2: what makes your test suite resilient and scalable over years
- 13:58Speaker 2: of development.
- 14:00Speaker 1: If the state of our test is robust and the
- 14:02Speaker 1: DOWM interactions are stable, what happens when something actually does
- 14:06Speaker 1: go wrong. Let's talk about debugging, because Eventually, the ghost
- 14:09Speaker 1: drivers can crash the car.
- 14:11Speaker 2: Oh inevitably, and debugging an E T E test is
- 14:14Speaker 2: entirely different from debugging a unit test, primarily because of
- 14:17Speaker 2: how Rails handles the database state.
- 14:19Speaker 1: This is a massive trap for new developers. You write
- 14:22Speaker 1: a test, it runs, and it saves the user to
- 14:25Speaker 1: the database. You might naturally assume that users still sitting
- 14:28Speaker 1: in your test database when the test is over, but
- 14:31Speaker 1: they aren't. They completely vanish, and.
- 14:33Speaker 2: We need to explain why they vanish. To ensure that
- 14:36Speaker 2: tests are isolated and repeatable, Rails wraps every single test
- 14:40Speaker 2: in a database transaction. When the test starts, the transaction opens.
- 14:44Speaker 2: When a test finishes, Rails issues a rollback command to
- 14:48Speaker 2: the database. Everything that happen, every user created, every order
- 14:51Speaker 2: placed is instantly erased. The database reverts to a completely
- 14:55Speaker 2: blank slate for the next test.
- 14:57Speaker 1: Which is brilliant for keeping the environment clean. It means
- 15:00Speaker 1: you literally cannot inspect the database after a failure to
- 15:03Speaker 1: see what went wrong. The evidence destroys itself the millisecond
- 15:06Speaker 1: the test crashes. It's a double edged sword, and this
- 15:08Speaker 1: clean slate causes another huge issue inside the test itself.
- 15:12Speaker 1: Redundant logic.
- 15:13Speaker 2: Yes, the state leakage problem within a single flow. Let's
- 15:17Speaker 2: say you write a script that tests the full registration process.
- 15:20Speaker 2: The ghost user fills out the sign up form and
- 15:22Speaker 2: submits it. By the design of your application, signing up
- 15:26Speaker 2: automatically logs that user in.
- 15:29Speaker 1: But if your very next line of test code says okay,
- 15:32Speaker 1: step two navigate to the login page, find the log
- 15:35Speaker 1: in button and click it, the test is going to blow.
- 15:37Speaker 2: Up because the log in button literally isn't rendering on
- 15:40Speaker 2: the screen. The user is already authenticated. The UI has
- 15:43Speaker 2: completely changed, right.
- 15:45Speaker 1: The test fails because it did its job too well
- 15:47Speaker 1: in the first step. You have to continually visualize the
- 15:50Speaker 1: state of the application at every microstep. And here's the
- 15:53Speaker 1: big question. If the database rolls back and we can't
- 15:55Speaker 1: look at the data, how do we visualize what went
- 15:58Speaker 1: wrong when a test fails invisibly in the background.
- 16:01Speaker 2: Well, usually E two E tests are run in a
- 16:03Speaker 2: headless mode, meaning the browser operates in memory without actually
- 16:07Speaker 2: drawing a physical window on your screen, which saves a
- 16:09Speaker 2: ton of processing power. But when things break, Rails provides
- 16:13Speaker 2: this amazing visual safety net that takes screenshot function.
- 16:17Speaker 1: Oh, this is such a fantastic tool. When an assertion fails,
- 16:20Speaker 1: say it just can't find the button, RAILS will automatically
- 16:23Speaker 1: capture an image of exactly what the headless browser saw
- 16:27Speaker 1: at that exact millisecond. It prepens the file name with
- 16:30Speaker 1: failures and the test name like failures loadgent test dot PNG,
- 16:35Speaker 1: and drops it right to your tom screenshots.
- 16:36Speaker 2: Fold is the definitive proof of the crash. You open
- 16:40Speaker 2: the image and instantly realize, Oh, a cookie consent pop
- 16:43Speaker 2: up covered the entire screen. That's why it couldn't click
- 16:46Speaker 2: the button. Yes, but there is a really tricky visual
- 16:49Speaker 2: quirt here regarding browser resolution.
- 16:52Speaker 1: Right by default, RAIL system tests run at a viewport
- 16:55Speaker 1: resolution of fourteen hundred by fourteen hundred pixels, which is,
- 16:58Speaker 1: you know, practically a square, but responsive web design is
- 17:01Speaker 1: entirely dictated by viewport dimensions. Yeah, if you are building
- 17:04Speaker 1: an application optimized for mobile phones, rendering it in a
- 17:07Speaker 1: fourteen hundred pixel wide window triggers your desktop CSS media queries.
- 17:11Speaker 1: It might look completely bizarre, or elements might be hidden entirely.
- 17:15Speaker 2: The test environment is essentially distorting the reality of the application.
- 17:18Speaker 1: To fix this, developers have to use a setup block.
- 17:21Speaker 1: This is code that executes before the test even begins.
- 17:24Speaker 1: You can use it to force capy bar to rescale
- 17:26Speaker 1: the browser to accurate physical dimensions, like if you want
- 17:30Speaker 1: to test the mobile experience, you tell the driver to
- 17:32Speaker 1: resize the window to five hundred by fifteen hundred.
- 17:35Speaker 2: Pixels, which forces the browser to apply your mobile flex
- 17:38Speaker 2: box rules or grid layouts, so the screenshot you get
- 17:42Speaker 2: on failure will actually look like a real smartphone screen.
- 17:46Speaker 2: It highlights that E two E testing isn't just a
- 17:48Speaker 2: binary logic check. It is a visual auditing tool. You
- 17:51Speaker 2: are explicitly testing your CSS breakpoints right alongside your Ruby code.
- 17:56Speaker 1: We are coming to the end of our deep dive
- 17:58Speaker 1: today and looking at the whole landscape of Ruby testing.
- 18:01Speaker 1: There are a few final powerful takeaways for optimizing this
- 18:04Speaker 1: whole process. We talked about how slow E two E
- 18:07Speaker 1: tests can be. One way developer speed things up is
- 18:10Speaker 1: by generating test data efficiently using things called fixtures and factories.
- 18:14Speaker 2: Oh, these are essential. If your test needs fifty users
- 18:16Speaker 2: in the database just to render a dashboard, you do
- 18:19Speaker 2: not want the ghost user manually typing out fifty registration
- 18:22Speaker 2: forms that takes forever.
- 18:23Speaker 1: Yeah, that'd be agonizing.
- 18:25Speaker 2: You use fixtures, which are static yamal files that inject
- 18:28Speaker 2: data directly into the database instantly, or you use factories
- 18:33Speaker 2: which dynamically construct objects in memory. It basically sets the
- 18:36Speaker 2: stage so the E two E test can focus solely
- 18:39Speaker 2: on the final interaction.
- 18:40Speaker 1: And once your setup as fast and your tests are
- 18:42Speaker 1: perfectly decoupled from third party libraries, you unlock the ultimate
- 18:46Speaker 1: optimization parallel testing right.
- 18:49Speaker 2: Because of the database transaction isolation we discussed earlier, modern
- 18:53Speaker 2: rails can actually utilize multiple CPU cores to run several
- 18:57Speaker 2: system tests at the exact same time in st background threads,
- 19:01Speaker 2: which vastly cuts down that total execution time.
- 19:04Speaker 1: So what does this all mean. It means the tools
- 19:06Speaker 1: to build a flawless user experience are entirely within our control,
- 19:10Speaker 1: provided we understand the quirks of the translation layer absolutely.
- 19:12Speaker 1: Before we go, let's do a quick educational review question
- 19:15Speaker 1: to reinforce the mechanics we've covered today for you. Imagine
- 19:19Speaker 1: you are writing a cappy bar test to fill out
- 19:21Speaker 1: a form and you tell the fill and locator to
- 19:23Speaker 1: find the ID take user password, but the test keeps crashing.
- 19:28Speaker 1: Based on what we discussed about how locators scan the
- 19:30Speaker 1: dom what single character do you need to delete to
- 19:33Speaker 1: make it work?
- 19:34Speaker 2: Hmmm? Think back to the difference between a CSS styling
- 19:37Speaker 2: selector and a semantic HTML locator.
- 19:40Speaker 1: If you said you needed to lead the hash symbol
- 19:42Speaker 1: the hashtag, you are exactly right. Locators look for raw strings,
- 19:46Speaker 1: not CSS syntax.
- 19:48Speaker 2: You know this raises an important question, though a provocative
- 19:51Speaker 2: thought for you to mull over as you build your
- 19:52Speaker 2: own applications. Throughout this deep dive, we've explored the meticulous
- 19:57Speaker 2: manual effort required by human developers to create this ghost user.
- 20:02Speaker 1: Yeah, a slot of work.
- 20:03Speaker 2: We have to write explicit code to mimic human behavior,
- 20:06Speaker 2: carefully decouple logic, managed race conditions, set up visual safety notes.
- 20:10Speaker 1: We are spending thousands of hours writing code just to
- 20:13Speaker 1: test other.
- 20:14Speaker 2: Code, precisely so as large language models and artificial intelligence
- 20:18Speaker 2: become deeply integrated into our IDEs. What happens when we
- 20:22Speaker 2: simply point an AI at our web page and say,
- 20:25Speaker 2: act like a chaotic user and find what's broken. Will
- 20:29Speaker 2: we reach a point where AI is dynamically generating the
- 20:32Speaker 2: E two E test scripts on the fly, or further,
- 20:35Speaker 2: will it be writing the user interface code A and
- 20:37Speaker 2: D simultaneously acting as the ghost user testing it, leaving
- 20:41Speaker 2: human developers entirely out of the loop.
- 20:43Speaker 1: It really makes you wonder who the true ghost in
- 20:45Speaker 1: the machine will be ten years from now, truly. But
- 20:48Speaker 1: until then we'll keep building our isolated tests, refining our
- 20:51Speaker 1: generic classes, and most importantly, making sure that even if
- 20:54Speaker 1: the back end engine is mathematically perfect, we never forget
- 20:57Speaker 1: to check if the front door actually opens. Thanks for
- 21:00Speaker 1: joining us on this deep dive, Keep learning, keep questioning,
- 21:03Speaker 1: and we'll see you next time.